当前位置:网站首页>How do I evaluate strings to numbers in Bash?
How do I evaluate strings to numbers in Bash?
2022-04-22 04:28:00 【wljslmz】
It's like Linux Everything in is a file ,bash Everything in is a string .
Technically speaking ,Bash There is no data type in , Essentially ,Bash Variables are just strings .
When you try in bash When performing arithmetic operations in , This can cause problems , The number you're trying to add will give you strange results .
ywmt@wljslmz:~$ sum=3+6
ywmt@wljslmz:~$ echo $sum
3+6
To be in bash Convert string to integer , Please pack them in $((X)) in , like this :
$((string))
If the string has a recognizable number , It will be treated as a number , The example I showed above can be corrected to :
ywmt@wljslmz:~$ sum=$((3+6))
ywmt@wljslmz:~$ echo $sum
9
You are in $((…)) Anything done in is considered an arithmetic operation .
Let's look at string to number conversion in more detail , So that you can go to bash Use arithmetic calculations in .
Convert a string variable to a number
Let's declare something that is basically a string “ Numbers ”.
ywmt@wljslmz:~$ a=11
ywmt@wljslmz:~$ b=3
You can check the assigned number :
ywmt@wljslmz:~$ echo $a
11
vimanyu@wljslmz:~$ echo $b
3
Now let's try adding the two numbers and storing the value in the third variable c:
ywmt@wljslmz:~$ c=$a+$b
ywmt@wljslmz:~$ echo $c
11+3
As you can see in the output above ,c It is also regarded as a string .
To make the addition successful , You need to expand by arithmetic “ transformation ” it :
ywmt@wljslmz:~$ c=$(($a+$b))
ywmt@wljslmz:~$ echo $c
14
Please note that ,c It's actually still a string , Until you implicitly use the same arithmetic extension again for another operation I'll discuss later .
ywmt@wljslmz:~$ c=$((5))
This is equivalent to :
ywmt@wljslmz:~$ c=5
d Let's use the third variable for another operation :
ywmt@wljslmz:~$ d=10
All these implicit declarations are still strings . This is different from the way I defined before a identical b.
Now proceed to the second operation :
ywmt@wljslmz:~$ e=$(($a+$b*$c-$d))
ywmt@wljslmz:~$ echo $e
16
In the above expression , First calculate b and c The product of the , Finally, calculate addition and subtraction .
As I mentioned before , You are in $((…)) Anything done between is considered arithmetic .
All these conversions apply to integers . It does not apply to floating point numbers , A number with a decimal point .
Try mixing actual strings and numbers
Mixing strings and integers is still safer than mixing coke and mint . however , It does produce a strange result .
ywmt@wljslmz:~$ sum=$((3+hello))
ywmt@wljslmz:~$ echo $sum
3
It's only when there are numbers ( As string ) Only when effective , If it's a pure string , It won't magically convert into some numbers , contrary , It will be ignored , As you can see in the example above .
Alternative methods : Use expr
You can also use this expr Tools to evaluate , But please note that it is not “ This machine ” Bash Program , Because you need coreutils install ( By default, in Ubuntu On ) As a separate package .
ywmt@wljslmz:~$ expr $a + $b \* $c - $d
16
版权声明
本文为[wljslmz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220426139839.html
边栏推荐
- 二叉树的层序遍历
- LeetCode_ Rectangle_ Difficulties_ 391. Perfect rectangle
- Pgpool II 4.3 Chinese Manual - introductory tutorial
- L1-049 ladder race seat allocation (20 points)
- [recent force deduction] verify binary search tree + convert ordered array into binary search tree
- What is the decimal type in MySQL?
- Mysql中的Decimal类型是什么?
- L1-050 penultimate string (15 points)
- Knowledge points related to compliance type
- Introduction to UVM Experiment 2
猜你喜欢

WebSocket学习

调用函数时,关于传参那些事~

Introduction to C - parallel programming

Introduction to Intel edge software center

Objectmapper, stop being new like a second product

Sr-te policy (Cisco) -- supplement
![[experience] Why does the IP address of HP printer start with 169.254](/img/e7/5068a565b57f066377fcb5754bc4bd.png)
[experience] Why does the IP address of HP printer start with 169.254

H7-TOOL发布固件V2.15, 脱机烧录增加瑞萨,合泰以及IS25WP全系列SPI FLASH等(2022-04-14)

How to check whether the version of oraclejdk is charged on the official website

如何在官网查看OracleJDK那个版本是否收费
随机推荐
【openEuler】Failed to download metadata for repo ‘EPOL‘: Cannot d
Sequence traversal of binary tree
Keras deep learning practice (2) -- using keras to construct neural network
想转行学测试,我劝你先了解一下这些内容
When calling a function, what about passing parameters~
L3-022 地铁一日游 (30 分)【floyd+dfs】
WinPcap get device list
How to use SQLite database file on SD card in Android studio
你的指针学到什么层次了?8个指针题目让你加深对指针的理解(下)-O-
Summary of using knowledge points
智慧用电安全管理系统
ES6 practical tips
【近日力扣】验证二叉搜索树+将有序数组转换为二叉搜索树
What is the decimal type in MySQL?
How to check whether the version of oraclejdk is charged on the official website
Job scheduling, intermediate scheduling, process scheduling
【近日力扣】重复的子字符串
H7-TOOL发布固件V2.15, 脱机烧录增加瑞萨,合泰以及IS25WP全系列SPI FLASH等(2022-04-14)
软件测试的测试方法你知道多少?
Intelligent power safety management system