$ () and ${} and $ (()) and (())
$ () and ${} and $ (()) and (()) $ () and ${} usage: in bash shell, $ () and ' ' (anti-quote) are used to do command replacement with (commands substitution). and $ () each shell can not be used, if you use bash2 words, certainly no problem ... see ${} bar ... It's actually used for variable substitution. In general, $var is not the same as ${var}. But using ${} will be more precise in defining the range of variable names. I'll use some examples to illustrate some of the psychic powers of ${}: Suppose we define a variable as: file=/dir1/dir2/dir3/my.file.txt we can replace it with ${} to get a different value: ${file#*/}: Take out the first one. /And its left string: dir1/dir2/dir3/my.file.txt${file##*/}: Take off the last/and its left string: my.file.txt${file#*.} : Take out the first one. And the string to the left: file.txt${file##*.} : Take out the last one. And its left string: txt${file%/*}: Take off the last bar/its right string:/dir1/dir2/dir3${file%%/*}: Remove the first/its right string: (null value) ${file%.*}: Take the last one. and its right string:/dir1/dir2/dir3/my.file${file%%.*}: Take out the first one. And to the right of the string:/dir1/dir2/dir3/my Memory method is: # is to remove the left (on the plate on the left)% is removed to the right (on the plate on the right side) single symbol is the smallest match; two symbols are the maximum match. ${file:0:5}: Extract the leftmost 5 bytes:/dir1${file:5:5}: Extracts the 5 consecutive bytes to the right of the 5th byte:/dir2 We can also replace the string in the value of the variable: ${file/dir/path}: The first dir Swap for Path:/path1/dir2/dir3/my.file.txt${file//dir/path}: Convert all dir to path:/path1/path2/path3/my.file.txt using ${} You can also assign values for different variable states(no setting, null value, non-null value): ${file-my.file.txt}: If $file is not set, use My.file.txt to return the value. (null and non-null values are not processed) ${file:-my.file.txt}: If the $file is not set or null, use My.file.txt to return the value. ${file+my.file.txt}: If the $file is set to a null or non-null value, the value is returned using My.file.txt. ${file:+my.file.txt}: If $file is a non-null value, My.file.txt is used to return the value. ${file=my.file.txt}: If $file is not set, My.file.txt is used to return the value, and the $file is assigned the value My.file.txt. (null and non-null values are not processed) ${file:=my.file.txt}: If $file is not set or null, use My.file.txt to return the value and assign the $file to My.file.txt. ${file?my.file.txt}: If $file is not set, the output is my.file.txt to stderr. (null and non-null values are not processed) ${file:?my.file.txt}: If $file is not set or null, the my.file.txt output to STDERR. (non-null values are not processed) the understanding above is that you must distinguish between the unset and null and the non-null of the three assignment states. In general,: null is not affected, if not with:, NULL is unaffected, if band: then even null is affected . And oh, ${#var} can calculate the length of the variable value: ${#file} can get 27, because/dir1/dir2/dir3/my.file.txt is just 27 bytes ... purpose of $ (()): It is used for integer arithmetic. In bash, the integer operation symbol for $ (()) roughly has these: +-*/: "Add, subtract, multiply, divide" respectively. %: Remainder Arithmetic & | ^!: "And, or, XOR, not", respectively. Example: Wangnc>a=5;b=7;c=2;wangnc>echo $a + $b 5 + 7waNgnc>echo $ ((A + b*c)) 19wangnc>echo $ ((((a*b)/C)) 17wangnc>echo $ (($a + $b * $c)) 19wangnc> the variable name in $ (()), Can be replaced with a $ symbol in front of it, or not, such as: $ (($a + $b * $c) can also get 19 results In addition, $ (()) can also be used for different carry (such as binary, octal, hexadecimal) for the operation, but the output is only a decimal: Echo $ ((16# 2a) results are used for 42 (16 decimal) (()): In fact, it is possible to redefine the value of a variable by simply (()), or as a testing:a=5; ((a++)) $a can be redefined as 6a=5; ((a--)) is a=4a=5; b=7; ((a < b)) a return value of 0 (true) is obtained. Common test symbols for (()) are as follows: <: less than;: Greater than <=: less than or equal to >=: greater than or equal to = =: equals! =: Not equal to
$ () and ${} and $ (()) and (())