I. Command substitution
A. in Bash, $ () and ' (anti-quote) are used for command substitution.
B. command substitution is similar to variable substitution, which is used to reorganize the command line, complete the command line in quotation marks, and then replace the result with the new command line.
Example 1:
$ echo Today is $ (date "+%y-%m-%d") today is 2014-07-01
Two. $ () and "(anti-quote)
(1) Difference
When ' CMD ' executes, the shell interprets everything in the " inside", then sends the final result of the explanation to the shell to execute. An error occurs if the final result of the interpretation is not the shell executable command. Of course, Just to get the content of cmd executed as text output, there's nothing wrong with it.
$ (cmd) at execution time, if CMD is a command, it is thrown directly to the shell to execute, and if the variable value, it is only the first layer of the literal interpretation of the shell to execute.
(2) Use occasions
"used in the resulting results will not be sent to the shell to explain, and only as the assignment directly, the text output and" "with the use;
$ () There will be a further explanation when the results are produced, both with and without, just to improve the readability of the program.
(3) In operation, both can achieve the corresponding effect, but the recommended use of $ (), for the following reasons:
' It's easy to mess with '.
In a multi-layered compound substitution, ' you have to have extra hops (backslashes), and $ () is more intuitive.
The disadvantage of $ () is that not all UNIX-like systems support this approach, and that the anti-quotes are basically used in all Unix shells, which are highly portable if written in shell script.
'(anti-quote) needs to be escaped. and $ () does not need to be escaped.
Example 2:
# cmd1 execution result as cmd2 parameter, and then CMD2 result as cmd3 parameter Cmd3 $ (CMD2 $ (CMD1)) # If you use anti-quote, direct reference is not possible, also need to do a jump-off processing cmd3 ' cmd2 \ ' cmd1\ '
Example 3:
echo ' echo ' \ \ ' #输出: Echo $ (echo ' \ \ ') #输出 \ \
Example 4:
Assume:
Vs100comntools=c:\program Files\Microsoft Visual Studio 10.0\common7\tools\
In Terminal input:
Echo ' echo ' ${vs100comntools} ' | Sed ' s#^.\+\\.\+#\1# '
Output
C:\Program Files\Microsoft Visual Studio 10.0\common7\tools\
echo $ (echo "${vs100comntools}" | sed ' s#^.\+\\.\+#\1# ')
Output
C:\Program Files\Microsoft Visual Studio 10.0\common7
$(...) The form of command substitution achieves the intended purpose, while the ' ... ' is not achieved because:
' ... ' to escape the ' sed ' ... \ \ escaped into the sed ' ... '. '
Change into
Echo ' echo ' ${vs100comntools} ' | Sed ' s#^.\+\\\\.\+#\1# '
or use $ (...) Can
Three. Expand
(1) ${} variable substitution
In general, there is no difference between $var and ${var}, but with ${} It is more accurate to define the range of variable names
$ a=b$ echo ${a}b# output: BB
(2) Take path, file name, suffix
Assign a variable to a path first, such as: File=/dir1/dir2/dir3/my.file.txt
Command to interpret the results
${file#*/} take off 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 its left file.txt
${file##*.} take off the last one. And its left-hand string txt
${file%/*} take out the last/and right string/dir1/dir2/dir3
${file%%/*} Remove the first/its right string (null value)
${file%.*} take off the last one. And the string on the right/dir1/dir2/dir3/my.file
${file%%.*} take off the first one. And the string on the right/dir1/dir2/dir3/my
Memory methods are as follows:
# is to get rid of the left side (on the keyboard # on the left side)
% is removed to the right (on the keyboard% on the right of $)
The single symbol is the minimum match; two symbols are the maximum match
* is used to match the unwanted character, which is the part that you want to remove.
Also specify the character separator number, with the * mate, decide which part to take
(3) Take substring and replace
Command to interpret the results
${file:0:5} extracts the leftmost 5 bytes /dir1
${file:5:5} extracts the 5th byte to the right of a contiguous 5 bytes/dir2
${file/dir/path} To 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
${#file} gets the variable length 27
(4) Assigning values to variables based on status
Command explanation notes
${file-my.file.txt} If $file not set, then use My.file.txt for return value null value and non-null value is not processed
${file:-my.file.txt} If the $file is not set or null, then use My.file.txt for return value non-null value is not processed
${file+my.file.txt} If $file is set to a null or non-null value, use My.file.txt as the return value is not set when not processed
${file:+my.file.txt} If $file is a non-null value, use My.file.txt for return value is not set and null value is not processed
${file=txt} If $file is not set, then the TXT is returned, and the $file is assigned to the TXT null value and the non-null value is not processed
${file:=txt} if $file not set or null value, then return TXT, $file assigned to TXT non-null value is not processed
${file?my.file.txt} If the $file is not set, then the My.file.txt output to STDERR null and non-null value is not processed
${file:?my.file.txt} If the $file is not set or null, the output of My.file.txt 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.
(5) array
A= "a b c def" # definition string a= (a b c def) # define character array
Command to interpret the results
${a[@]} returns an array of all elements A b c def
${a[*]} ditto A b C def
${a[0]} returns an array of the first element a
${#A [@]} returns the total number of array elements 4
${#A [*]} ibid. 4
${#A [3]} returns the length of the fourth element, which is the length of def 3
A[3]=XYZ is the fourth group number redefined as XYZ
(6) $ (()) and integer operations
Integer operation symbols in bash
Symbol function
+-*/ Add, subtract, multiply, divide respectively
% remainder operation
& | ^ ! "and, or, XOR, not", respectively
The variable name in $ (()) can be replaced with the $ symbol in front of it or not.
$ a=5;b=7;c=2$ echo $ ((a+b*c)) 19$ echo $ (($a + $b * $c)) 19
(7) Binary conversion
$ (()) You can turn other binary into decimal number display. Use the following:
echo $ ((N#XX))
where n is the binary, XX is the value of the binary, the command can be executed after the number of decimal values.
$ echo $ ((2#110)) # binary goto decimal 6$ echo $ ((16#2A)) # hexadecimal to decimal 42$ echo $ ((8#11)) # Octal to decimal 9
(7) (()) redefine variable values
$ a=5;b=7$ ((a++)); Echo $a 6$ ((a--)); Echo $a 5$ ((a<b)); Echo $?0
When using (()) as an integer test, do not mess with the [] integer test.
This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1788324
command in Shell replaces $ (...) The difference and connection with the post-reference ' ... '