Distinguish back quotation marks, $() and ${} in shell

Time:2021-1-22

1. Back quotation marks and $() are used for command substitution

Reverse quotation marks and $() have the same function. They are used for command substitution, that is, to complete the execution of the referenced command and replace the result, which is similar to variable substitution. For example:

echo `date '--date=1 hour ago' +%Y-%m-%d-%H`
#Or
echo $(date '--date=1 hour ago' +%Y-%m-%d-%H)

The output is the same:

2019-02-02-09。

When writing shell scripts, $() is recommended for the following reasons:
(1) Reverse quotation marks and single quotation marks are similar in appearance and easy to be confused;
(2) In the multi-level compound substitution, the inner counter quotation marks need to be escaped (\ ‘), while $() is more intuitive. For example, the following command format is wrong:


command1 `command2 `command3``

The original intention is to replace command3 in ‘command2’ and ‘command3’ to command2 for processing, and then pass the result to command1 for processing. However, the real result in the command line is divided into two parts: “command2” and “Command 2”. The correct input should be as follows:

command1 `command2 \`command3\``
#Or change to $()
command1 $(command2 $(command3))

(3) There is a special treatment for backslashes in back quotation marks. When using anti protocol to escape shell special characters, two backslashes are needed, and$()Only one backslash is required in. For example, the following script needs output$HOMEInstead of the content of the environment variable home, you need to$Double backslashes are used to escape,$()Only one backslash is required in.

VAR1 = ` echo \ $home '# cannot escape the $character with a backslash
var2=`echo \$HOME`
var3=$(echo $HOME)
echo $var1
echo $var2
echo $var3

Script execution result:

/root
$HOME
$HOME

To sum up, $() is an old usage and $() is a new usage. It is recommended to use $() in both study and practical work.

2. ${} is used for variable substitution

2.1 direct variable substitution

Normally,$varAnd${var}It doesn’t make any difference, but it does${ }The scope of variable name will be defined more accurately, for example:


A="dablelv"
echo $AB

Originally, I was going to$ABut on the command line, the real result is that only the value named AB will be replaced. If used${}No problem.


echo ${A}B

2.2 special variable substitution

${}In addition to directly replacing variable contents, there are some special functions for string variables. Suppose we define a string variable as:


file="/dir1/dir2/dir3/my.file.txt"

2.2.1 ${:}And${::}For string extraction

String extraction can use${:}And${::}

(1)${var:n}
If n is a positive number and N starts from 0, it means that all characters from the nth character to the end of VaR are extracted. If n is a negative number, the absolute value of N on the last side of the string is extracted. When using, add a space after the colon or an arithmetic expression or the whole num with brackets, such as${var: -2}${var:1−3}or${var:(-2)}Both indicate that the last two characters are extracted.

${ file:1 }: extract the first character and all characters after it: dir1 // dir2 / dir3/ my.file.txt
${file: - 3}: extract the last three characters. Note that a space is added after the colon: TXT
${ file:1-4 }: extract the last three characters, no need to add space after colon: TXT
${file: (- 3)}: extract the last three characters. There is no need to add space after colon: TXT

(2)${var:n1:n2}
${var:n1:n2}It is used to extract N2 characters after N1, where N1 and N2 start from 0.

${ file:0 : 5}: extract 5 leftmost characters / dir1
${ file:5 : 5}: extract 5 consecutive characters to the right starting from the 5th character / dir2

2.2.2 ${/}And${//}Replacement for string pattern matching

${var/pattern/pattern}Represents to replace the first matching pattern of the VaR string with another pattern. The original variable is not changed.

${file / dir / path}: replace the first dir with path / path1 / dir2 / dir3/ my.file.txt
${file // dir / path}: replace all dirs with path / path1 / path2 / path3/ my.file.txt

2.2.3${#}${##}${%}And${%%}Truncation for string pattern matching

You can filter out the string that meets the specified rules without changing the original variable.

${file # * /}: remove the first / and its left string: dir1 / dir2 / dir3/ my.file.txt
${file # * /}: remove the last / and its left string: my.file.txt
${file # *.}: remove the first. And the string to the left of it: file.txt
${file # *.}: remove the last. And its left string: TXT
${file% / *}: remove the last / and the string to the right of / dir1 / dir2 / dir3
${file%% / *}: remove the first / and its right string: (null value)
${file%. *}: remove the last. And the string to the right of it / dir1 / dir2 / dir3/ my.file
${file%%. *}: remove the first. And the string to the right of it / dir1 / dir2 / dir3 / my

The method of memory is as follows

#It’s on the left (on the checkerboard, to the left of the dollar)
%Yes, remove the right side (on the checkerboard,%, to the right of $)
One symbol is the minimum match and two symbols are the maximum match.

On variable substitution${}Other special usages are not detailed here, for referenceShell special characters

The above is the detailed content of distinguishing the back quotation marks, $() and ${} in shell. For more information about the back quotation marks, $() and ${} in shell, please pay attention to other related articles of developer!