command Substitution
In Bash, $ () and ' (inverted quotes) are used for command substitution.
The command substitution is similar to the variable substitution, which is used to reorganize the command line, complete the command line in the quotation marks, and then replace the result with the new command line.
Exp 1
$ echo today are $ (date "+%y-%m-%d") today is
2014-07-01
$ () and "
In operation, both of these are to achieve the corresponding effect, but the proposed use of $ (), for the following reasons: "Very easy and" confusing, especially for beginners. In multiple layers of compound substitution, the ' must have an extra jump (backslash), and $ () is more intuitive. Finally, the downside of $ () is that not all UNIX-like systems support this approach, but the inverted quotes are definitely supported.
Exp 2
# The CMD1 execution result as the CMD2 parameter, and then the CMD2 result as the CMD3 parameter
cmd3 $ (cmd2 $ (CMD1))
# If you are using an inverted quote, a direct reference is not possible, and you need to do a jump-off process
cmd3 ' cmd2 ' Cmd1\ '
${} variable substitution
In general, $var and ${var} is no different, but with ${} will be more precise definition of the scope of the variable name
$ a=b
$ echo ${a}b
BB
The
takes the path, the filename, the suffix
to first assign a variable to a path, as follows:
File=/dir1/dir2/dir3/my.file.txt
command |
explain |
results |
${f ile#*/} |
takes off the first/and its left string |
dir1/dir2/dir3/my.file.txt |
${file##*/} |
take off the last one/and its left string |
my.file.txt |
${file#*.} |
take off the first one. and its left string |
file.txt |
${file##*.} |
take off the last one. and its left string |
txt |
${file%/*} |
take off the last one/and the string to the right |
/dir1/dir2/dir3 |
${file%%/*} |
take off the first/and its right string |
(null) |
${file%.*} |
take off the last one. and its right-hand string |
/dir1/dir2/dir3/my.file |
${file%%.*} |
takes the first one. and its right-hand string |
/dir1/dir2/dir3/my |
The memory method is as follows: # is to remove the left (on the keyboard # on the left of $)% is to remove the right (on the keyboard% on the right side of the $) a single symbol is the smallest match; two symbols is the maximum match * is used to match the character, that is to remove the part also has the specified character separator, and * with, decide which part to take
pick substring and replace
Command |
explain |
Results |
${file:0:5} |
Extract the leftmost 5 bytes |
/dir1 |
${file:5:5} |
Extracts 5 consecutive bytes to the right of the 5th byte |
/dir2 |
${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 |
${#file} |
Get variable length |
27
|
Assigning values to variables based on state
Command |
explain |
Notes |
${file-my.file.txt} |
If the $file is not set, use My.file.txt as the return 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 as the return value |
Non null value is not processed |
${file+my.file.txt} |
If the $file is set to a null or Non-null value, the my.file.txt is used as the return value |
No processing when not set |
${file:+my.file.txt} |
If the $file is Non-null, the value is returned using My.file.txt |
Not set and null value not to be processed |
${file=txt} |
If $file does not set, then return TXT, and the $file assigned to TXT |
Null and Non-null values are not processed |
${file:=txt} |
If the $file is not set or null value, then return TXT, the $file assigned to TXT |
Non null value is not processed |
${file?my.file.txt} |
If the $file is not set, output the My.file.txt to STDERR |
Null and Non-null values are not processed |
${file:?my.file.txt} |
If the $file is not set or null, output the my.file.txt to stderr |
Non null value is not processed
|
Tips:
The above understanding is, you must distinguish Chu unset and null and non-null these three kinds of assignment state. In general, NULL is not affected if NULL is involved, and null is affected if the band:
Array
A= "a b C def" # define string
a= (a b c def) # define character array
Command |
explain |
Results |
${a[@]} |
Returns all elements of an array |
A b c def |
${a[*]} |
Ditto |
A b c def |
${a[0]} |
Returns the first element of an array |
A |
${#A [@]} |
Returns the total number of elements in an array |
4 |
${#A [*]} |
Ditto |
4 |
${#A [3]} |
Returns the length of the fourth element, that is, the length of the Def |
3 |
A[3]=xyz |
is to redefine the fourth group number to XYZ |
|
$ (()) and integer operations
integer operation symbol in bash
symbols |
function |
+ - * / |
Add, subtract, multiply and divide respectively |
% |
Remainder operations |
& | ^ ! |
"And, or, XOR, not", respectively.
|
The name of the variable in $ (()) can be replaced with the $ symbol before it, or it may not be used.
$ a=5;b=7;c=2
$ echo $ ((a+b*c))
$ echo $ (($a + $b * $c))
19
in-process conversion
$ (()) can be turned into decimal numbers to display. Usage is as follows:
echo $ ((N#XX))
where n is the system, XX is the value of the system, the command can be executed after the conversion of the number of decimal values.
$ echo $ ((2#110)) # Binary to decimal
6
$ echo $ ((16#2a)) # hexadecimal decimal
$ echo $ ((8#11)) # Octal Turn decimal
9
(()) Redefining 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 confuse the integer test with [].