Quotes --- single quotes, double quotes and backslashes

Double quotes

The text will be displayed in the form of characters, and part of the expansion will be filtered, but the parameter expansion, arithmetic expansion and command substitution are still valid ($,``,\)

apostrophe

Suppress all expansion

Backslash

Escape characters, which can limit the expansion of some double quotes

Example 1:

Variable nesting: can be achieved by suppressing the outer expansion and eval command

[portal@ccodtk test]$ cat test1.sh 
#!/bin/bash

set -xueo pipefail

a_config="{a1,a2}"

app1_config=\${${1}_config}
echo $app1_config

eval app_config=\${${1}_config}
echo $app_config
--------------------------
[portal@ccodtk test]$ bash test1.sh a
+ a_config='{a1,a2}'
+ app1_config='${a_config}'
+ echo '${a_config}'
${a_config}
+ eval 'app_config=${a_config}'
++ app_config='{a1,a2}'
+ echo '{a1,a2}'
{a1,a2}

analysis:

  • app1_configAfter adding the transfer character to the outer layer of the variable , it refers to the expansion of the outer layer, so as to realize the app1_configcontent of the variable form.
  • Through the command eval, the content of this variable in text form is parsed by bash to show the final content

Example 2:

Single quotes suppress all transfers, double quotes do not suppress transfers of parameter expansion

[root@qcteam-ciserver resources]# echo $USER
root
[root@qcteam-ciserver resources]# pwd
/home/docker/Cubes/dcmsSecurity/src/main/resources
[root@qcteam-ciserver resources]# ssh [email protected] 'pwd; echo $USER'
/home/portal
portal
[root@qcteam-ciserver resources]# ssh [email protected] "pwd; echo $USER"
/home/portal
root
[root@qcteam-ciserver resources]# ssh [email protected] "pwd; echo \$USER"
/home/portal
portal

analysis:

  • Through the first two steps, you can see the current environment information;
  • The third step shows the suppression and expansion function of single quotation marks, which will pass all the content inside the quotation marks as they are;
  • The fourth step shows the function of the inconsistent parameter expansion of double quotes, $USERwhich has been parsed as the current user's value before being passed.
  • The fifth step shows the suppression of expansion of backslashes in double quotes

Guess you like

Origin blog.csdn.net/jjt_zaj/article/details/113055930