$ () and ' (anti-quote)
In the bash shell, $ () and "(anti-quote) are used for 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 quotation marks, then replace the result, then reorganize the command line.
For example:
[code]$ echo the last Sunday was $ (date-d "last Sunday" +%y-%m-%d) [/code]
This makes it 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,
Then pass the result 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, a $ () is no problem:
[Code]command1 $ (Command2 $ (COMMAND3)) [/code]
As long as you like, how many layers of replacement are no problem ~ ~ ~ ^_^
$ () Insufficient:
1. ' Basically can be used in all Unix shells, if written in shell script, its portability is relatively high.
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}.
But 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 after,
But on the command line, the real result is only to mention the value of the variable name AB ...
If you use ${} There is no problem:
$ echo ${a}b
Bb
However, if you only see ${} can only be used to define the variable name, then you really underestimate bash!
If you are interested, you can first refer to the essence of the CU version of the 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 as:
File=/dir1/dir2/dir3/my.file.txt
We can replace each other with ${} to get different values:
${file#*/}: Take out the first/its left string: dir1/dir2/dir3/my.file.txt
${file##*/}: Take out the last/and 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 the string to the left: txt
${file%/*}: Take off the last bar/its right string:/dir1/dir2/dir3
${file%%/*}: Remove the first/its right string: (null value)
${FILE%.*}: Take off the last one. And the string to the right:/dir1/dir2/dir3/my.file
${FILE%%.*}: Take out the first one. And the string to the right:/dir1/dir2/dir3/my
The methods of memory are:
[list]# is to remove the left side (on the plate # on the left of the $)
% is removed to the right (on the plate% on the right of the $)
The single symbol is the minimum match, and the two symbol is 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}: Change all dir to Path:/path1/path2/path3/my.file.txt
With ${} You can also assign values to different variable states (no settings, null values, non-null values):
${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. (Non-null value is not processed)
${file+my.file.txt}: If the $file is set to a null value or a non-null value, the value is returned using My.file.txt. (not processed when not set)
${file:+my.file.txt}: If $file is a non-null value, use My.file.txt as the return value. (No processing when not set and null value)
${file=my.file.txt}: If $file not set, use My.file.txt as the return value, and assign the $file to 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 as the return value, and assign the $file to My.file.txt. (Non-null value is not processed)
${file?my.file.txt}: If the $file is not set, the My.file.txt output to STDERR. (null and non-null values are not processed)
${file:?my.file.txt}: If the $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} to 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.
Generally, a variable such as a= "a b C def" simply replaces the $A with a single string,
But instead of a= (A b C def), the $A is defined as the number of groups ...
Bash's group number substitution method can be referred to as follows:
${a[@]} or ${a[*]} to get A b C def (total number of groups)
${a[0]} can get a (number of first group), ${a[1]} is the second group number ...
${#A [@]} or ${#A [*]} to get 4 (total number of groups)
${#A [0]} can get 1 (that is, the length of the first group (a), ${#A [3]} can get 3 (fourth group number (def) length)
A[3]=XYZ is the fourth group number redefined 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 operation
& | ^!: "And, or, XOR, not", respectively.
Cases:
$ a=5; b=7; c=2
$ echo $ ((a+b*c))
19
$ echo $ (((a+b)/C))
6
$ echo $ (((a*b)%c))
1
The variable name in $ (()) can be replaced with the $ symbol in front of it, or it can be used, 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 all decimal:
echo $ ((16#2A)) result is 42 (16 decimal)
Let's take a look at it in a practical example:
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++)) to redefine $a to 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 (()) include the following:
<: Less than
: Greater Than
<=: Less than or equal to
>=: greater than or equal to
= =: equals
! =: Not equal to
Transfer from http://blog.chinaunix.net/uid-14351756-id-2820651.html
The difference between $ (()), $ (), ${} in the shell