The difference between double quotes, single quotes, and backticks in shell scripts

iamlaosong文

Recently, I want to compile a shell script to process data, and I need to detect whether the data file exists. The file name contains the date, so it is necessary to generate the date of the last few days in order to detect whether the file exists. See how to use double quotation marks, single quotation marks, Backticks:

[plain]  view plain copy  
  1. OPDATE=`date -d '-1 day' +%Y%m%d`  
  2.   
  3. for i in $(seq 10)  
  4. do  
  5.    FILEDATE=`date -d "-$i day" +%Y%m%d`  
  6.    echo ${FILEDATE}  
  7. done  

1. Characters enclosed by single quotation marks appear as ordinary characters. After the special characters are enclosed in single quotation marks, they will lose their original meaning and will only be interpreted as ordinary characters. Such as '-1 day' in the routine, and another example:

$ string='$PATH'
$ echo $string
$PATH
$
visible$ retains its meaning and appears as a normal character.

2. The characters enclosed by double quotation marks, except $ (dollar sign), \ (backslash), ' (single quotation mark), and " (double quotation mark), are still special characters and retain their special functions. , the rest of the characters are still treated as ordinary characters. For "$", it is to replace the variable and $ with the value of the variable specified after it; for "\", it is an escape character, which tells the shell not to The latter character is treated as a normal character only. It is conceivable that there are only four characters $, \, ' and " themselves that need to be preceded by "\" in double quotation marks. For the "", if there is no "\" in front of it, the Shell will match it with the previous ". For example, "-$i day" in the routine will convert $i to the value it represents with the loop.

3. The key corresponding to the backtick (`) character is generally located in the upper left corner of the keyboard. Do not confuse it with the single quotation mark ('). A string enclosed in backticks is interpreted by the shell as a command line. When executed, the shell executes the command line first and replaces the entire backticks (including the two backticks) with its standard output. For example, `date -d '-1 day' +%Y%m%d` in the routine is to assign the result of this command to the variable OPDATE.

4. Backticks can also be nested. Note, however, that inner backticks must be escaped with a backslash (\) when nested. For example:
$ abc=`echo The number of users is \`who| wc -l\``
$ echo $abc
The number of users is 2
$
5. You can also use shell specials on the command line between backticks character. In order to get the result of the command in ``, the shell actually has to execute the command specified in ``. When executed, the special characters in the command, such as $, ", ?, etc., will have special meanings, and `` can contain any legal shell command, such as:
$ ls
note readme.txt Notice Unix.dir
$ TestString="`echo $HOME ` ` ls [nN]*`"
$ echo $TestString
/home/yxz note Notice

Finally, a digression: Backticks are an old usage, $() is the new usage, like $(seq 10) in a routine. Whether in learning or in real work, $() is the recommended usage. So the above usage can be changed to:

$ TestString="$(echo $HOME) $(ls [nN]*)"

$ echo $TestString

/home/yxz note Notice

For the execution of the script, you can use the following command to analyze:

sh -x test.sh

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324731681&siteId=291194637