The difference between $ (()) in the shell and $ () and ${}
Http://blog.chinaunix.net/uid-14351756-id-2820651.html
$ () and ' (back quotes) in the bash shell, $ () and "(anti-quote) are used to do command substitution.
The so-called command substitution is similar to the variable substitution we learned in the fifth chapter, which is used to reorganize the command line: * Complete the command line in the quotation marks, then replace the results and reorganize the command line. For example: [code]$ Echo, the last Sunday is $ (date-d "Sunday" +%y-%m-%d) [/code] so it's easy to get the date of the previous Sunday ... ^_^
Reasons for Using $ ():
1, ' It's easy to mess with ' (single quotes), especially for beginners. Sometimes in some strange glyphs, the two symbols are identical (vertical two points). Of course, an experienced friend can make a difference at a glance. But what if it is better to avoid confusion and he le? ^_^
2, in the multi-level compound substitution, ' need extra jump (\ ') processing, and $ () is more intuitive. For example: This is wrong: [code]command1 ' Command2 ' Command3 ' [/code] The original intention is to Command2 ' Command3 ' first to change the Command3 to command 2 processing, and then the result Pass to Command1 ' Command2 ... ' to deal with it. However, the real results are divided into ' command2 ' and ' two segments ' in the command line. The correct input should be as follows: [code]command1 ' command2 \ ' command3\ ' [/code]
Otherwise, to $ () there is no problem: [Code]command1 $ (Command2 $ (COMMAND3)) [/code] as long as you like, do how many layers of replacement is no problem ~ ~ ~ ^_^
$ () Insufficient: 1. ' Basically it can be used in all Unix shells, and if written in shell script, it's more portable. and $ () every shell can be used, I can only tell you, if you use bash2 words, certainly no problem ... ^_^
${} is used for variable substitution. In general, $var is not the same as ${var}. However, using ${} will be more precise in defining the range of variable names, for example: $ A=B $ echo $AB originally intended to replace the results of the $A, and then fill a B-letter later, but on the command line, the real result is only to mention the variable name of the value of AB out ... If you use ${} then no problem: $ echo ${a}b BB
However, if you only see ${} can only be used to define the variable name, then you are too underestimated bash! If you are interested, you can refer to the essence of CU this edition article: http://www.chinaunix.net/forum/viewtopic.php?t= 201843
For the sake of completeness, I'll use some examples here to illustrate some of the supernatural powers of ${}: Suppose we define a variable: file=/dir1/dir2/dir3/my.file.txt we can replace it with ${} to get a different value: ${file#*/} : Take out the first/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) ${file%.*}: Take off 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: [list]# is to remove the left (on the plate on the left)% is to remove the right (on the plate on the right of the $) single symbol is the smallest match; two symbols are the maximum match. [/list] ${file:0:5}: Extract the leftmost 5 bytes:/dir1 ${file:5:5}: Extracts the 5th byte to the right of 5 consecutive bytes:/DIR2
We can also replace the string in the value of the variable: ${file/dir/path}: Change the first dir to Path:/path1/dir2/dir3/my.file.txt ${file//dir/path}: Swap all dir for path: /path1/path2/path3/my.file.txt
With ${} You can also assign values to 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 value is not processed)
Tips: The above understanding is that you must distinguish between Chu unset and null and non-null these three kinds of assignment states. In general,: null is not affected if not with: null is also affected if the band: null.
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 ...
Next, let's take a little bit of Bash's group number (array) processing method. In general, a variable such as a= "a b C def" simply replaces the $A with a single string, but instead a= (a b C def), the $A is defined as the number of groups ... bash group number substitution method can refer to the following method: ${a[@]} or ${a[*]} to get A b C def (all groups) ${a[0]} can get a (first group number), ${a[1]} is the second group number ... ${#A [@]} or ${#A [*]} can get 4 (the total number of groups) ${#A [0]} can get 1 (that is, the length of the first group (a), ${ #A [3]} can get 3 (the length of the fourth Group (DEF)) A[3]=XYZ is to redefine the fourth number of groups as XYZ ...
Well, at the end, let's introduce the use 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: $ a=5; b=7; C=2 $ echo $ ((A+B*C)) $ echo $ (((a+b)/C)) 6 $ Echo $ (((a*b)%c)) 1
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 be used for different carry (such as binary, octal, hex) for the operation, but the output is only decimal: Echo $ ((16#2A)) result is 42 (16 decimal) with a practical example to see it: if the current umask is 022, Then the permissions for the new file are: $ umask 022 $ echo "obase=8;$ ((8#666 & (8#777 ^ 8#$ (umask)))" | BC 644
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 6 a=5; ((A –)) is a=4 a=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
Shell Script "$ ()", "${}" learning