$ () and ' (anti-quote)
In the bash shell, $ () and "(anti-quote) are used for command substitution.
$ echo the last Sunday was $ (date-d "last Sunday" +%y-%m-%d)
Get the date of the previous Sunday
The reason for using $ ()
- ' (anti-quotes) is easy to mix with ' (single quotes). Sometimes in some strange glyphs, the two symbols are identical (vertical two points).
In multi-layered compound replacements, and (反引号)须要额外的跳脱(\
$ () is more intuitive. For example:
Command1 command2
command3 ` 原本的意图是在command2
command3 中先将command3替换出来给command2处理,然后再将结果传给command1
command2 ... 来处理。 然而,真正的结果在命令行中却是分成了
Command2 与
' two paragraphs.
The correct input should be as follows:
Command1 command2 \
Command3 '
Change to $ () at a glance:
command1 $(command2 $(command3))
$ () Insufficient
~ (') can basically be used in all Unix shells, if written in shell script is more portable. and $ () not every shell can be used.
${} used for variable substitution
In general, the $var works 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 result of a $ A and then a B-letter followed, but on the command line, the real result is only replace the value of the variable name is AB out.
Using ${} is no problem:
$ echo ${A}BBB
Some psychic powers of ${}
Define a variable:
File=/dir1/dir2/dir3/my.file.txt
You can use ${} to replace each other to get different values:
${file#*/} 拿掉第一个 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt${file##*/} 拿掉最后一个 / 及其左边的字符串:my.file.txt${file#*.} 拿掉第一个 . 及其左边的字符串:file.txt${file##*.} 拿掉最后一个 . 及其左边的字符串:txt${file%/*} 拿掉最后一个 / 及其右边的字符串:/dir1/dir2/dir3${file%%/*} 拿掉第一个 / 及其右边的字符串:(空值)${file%.*} 拿掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file${file%%.*} 拿掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my
Methods of Memory:
# 去掉左边(键盘上 # 在 $ 的左边)% 去掉右边(在键盘上 % 在 $ 的右边)单一符号是最小匹配,两个符号是最大匹配。${file:0:5} 提取最左边的 5 个字节:/dir1${file:5:5} 提取第 5 个字节右边的连续 5 个字节:/dir2
You can also replace the string in the value of the variable:
${file/dir/path} 将第一个 dir 替换为 path:/path1/dir2/dir3/my.file.txt${file//dir/path} 将全部 dir 替换为 path:/path1/path2/path3/my.file.txt
With ${} You can also assign values to different variable states (not set, NULL, non-null values):
${file-my.file.txt} 若 $file 未设定,则使用 my.file.txt 作传回值。(空值及非空值时不作处理) ${file:-my.file.txt} 若 $file 未设定或为空值,则使用 my.file.txt 作传回值。(非空值时不作处理)${file+my.file.txt} 若 $file 设为空值或非空值,均使用 my.file.txt 作传回值。(未设定时不作处理)${file:+my.file.txt} 若 $file 为非空值,则使用 my.file.txt 作传回值。(未设定及空值时不作处理)${file=my.file.txt} 若 $file 未设定,则使用 my.file.txt 作传回值,同时将 $file 赋值为 my.file.txt。 (空值及非空值时不作处理)${file:=my.file.txt} 若 $file 未设定或为空值,则使用 my.file.txt 作传回值,同时将 $file 赋值为 my.file.txt。 (非空值时不作处理)${file?my.file.txt} :若 $file 未设定,则将 my.file.txt 输出至 STDERR。(空值及非空值时不作处理)${file:?my.file.txt} :若 $file 未设定或为空值,则将 my.file.txt 输出至 STDERR。(非空值时不作处理)
The above understanding is, must distinguish Chu unset with null and non-null these three kinds of assignment state.
In general, NULL is concerned, without: null is not affected, if band: then even null is affected.
${#var} to calculate the length of the variable value:
${#file} can get 27,/dir1/dir2/dir3/my.file.txt just 27 bytes.
Bash Arrays (array) processing method
In general, 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 an array.
The array substitution method for Bash can be found in the following ways:
${A[@]} 或 ${A[*]} 得到 a b c def(全部数组)${A[0]} 得到 a (第一个元素),${A[1]} 第二个...${#A[@]} 或 ${#A[*]} 得到 4 (数组数量)${#A[0]} 得到 1 (第一个元素 a 的长度),${#A[3]} 得到 3 (第四个元素 def 的长度)A[3]=xyz 将第四个元素重新定义为 xyz
Purpose of $ (())
Used for integer arithmetic. In bash, the integer operation symbol for $ (()) roughly has these:
+ - * / 加、减、乘、除% 余数运算& | ^ ! AND、OR、XOR、NOT运算
Example:
$ 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 also be preceded by a $ symbol: $ (($a + $b * $c)) can also get 19 results.
In addition, $ (()) can also be used for different binary (such as binary, octal, hexadecimal) operations, but the output is only decimal.
echo $ ((16#2A)) result is 42 (16 decimal)
To give a practical example:
The current umask is 022, and the permissions for the new file are:
$ umask 022$ echo "obase=8; $(( 8#666 & (8#777 ^ 8#$(umask)) ))" | bc644
In fact, it is possible to redefine the value of a variable by simply (()), or as a testing:
a=5; ((a++)) 将 $a 重定义为 6a=5; ((a–)) a=4a=5; b=7; ((a < b)) 会得到 0 (true) 的返回值
Common test symbols for (()) have the following:
< 小于> 大于<= 小于或等于>= 大于或等于== 等于!= 不等于
Reprinted from: http://blog.csdn.net/tg5156/article/details/19406275
The difference between $ (()) in the shell and $ () and ${}