Linux Shell – 单引号 [ ‘ ]、双引号 [ ” ] 和反引号 [ ` ]
Linux Shell – 单引号 [ ‘ ]、双引号 [ ” ] 和反引号 [ ` ]
Bash 只有一种数据类型,即字符串。用户输入的数据,Bash 都视为字符串。
1. 单引号 ('
)
单引号 ('
) 用于保留字符的字面含义,特殊字符在单引号里面,都会变为普通字符,例如 *
、$
、反斜杠 (\
) 等。
(base) yongqiang@yongqiang:~/bash_work$ ls -l
total 0
-rwxr-xr-x 1 yongqiang yongqiang 231 May 13 23:24 bash_script.sh
-rw-r--r-- 1 yongqiang yongqiang 0 May 15 09:31 yongqiang.txt
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo *
bash_script.sh yongqiang.txt
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '*'
*
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "*"
*
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $
$
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '$'
$
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "$"
$
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $PATH
/home/yongqiang/bin:/home/yongqiang/miniconda3/bin:/home/yongqiang/miniconda3/condabin:......:/home/yongqiang/software/platform-tools/
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '$PATH'
$PATH
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $PATH
/home/yongqiang/bin:/home/yongqiang/miniconda3/bin:/home/yongqiang/miniconda3/condabin:......:/home/yongqiang/software/platform-tools/
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $USER
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '$USER'
$USER
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "$USER"
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $((5 + 1))
6
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '$((5 + 1))'
$((5 + 1))
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "$((5 + 1))"
6
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $(echo $USER)
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '$(echo $USER)'
$(echo $USER)
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "$(echo $USER)"
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
反斜杠在单引号中变成了普通字符。如果在单引号之中,还要使用单引号,不能使用转义,需要在外层的单引号前面加上一个($
),然后再对里层的单引号转义。
(base) yongqiang@yongqiang:~/bash_work$ echo '\'
\
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo 'It's me!' bash: !': event not found
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo 'It\'s me!' bash: !': event not found
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $'It\'s me!'
It's me!
(base) yongqiang@yongqiang:~/bash_work$
在双引号之中使用单引号。
(base) yongqiang@yongqiang:~/bash_work$ echo "It's me!"
It's me!
(base) yongqiang@yongqiang:~/bash_work$
2. 双引号 ("
)
双引号 ("
) 比单引号 ('
) 宽松。在双引号中,反引号 (`
)、$
、反斜杠 (\
) 三个特殊字符会被 Bash 自动扩展。$
符号用于引用变量,反引号 (`
) 用于执行子命令。反斜杠 (\
) 在双引号之中用来转义。
(base) yongqiang@yongqiang:~/bash_work$ echo ?
?
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '?'
?
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "?"
?
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $SHELL
/bin/bash
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '$SHELL'
$SHELL
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "$SHELL"
/bin/bash
(base) yongqiang@yongqiang:~/bash_work$
使用反斜杠,在双引号之中插入双引号,或者插入反斜杠本身。
(base) yongqiang@yongqiang:~/bash_work$ echo '\\'
\\
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "\\"
\
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo ""yongqiang""
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "\"yongqiang\""
"yongqiang"
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "It's me!"
It's me!
(base) yongqiang@yongqiang:~/bash_work$
Bash 正常情况下会将换行符解释为命令结束,但是换行符在双引号之中就失去了这种特殊作用,只用来换行。可以输入多行,echo
命令会将换行符原样输出,显示的时候正常解释为换行。
(base) yongqiang@yongqiang:~/bash_work$ echo "yong > qiang"
yong
qiang
(base) yongqiang@yongqiang:~/bash_work$
如果文件名中包含空格,需要使用双引号或单引号,将文件名放在里面。
(base) yongqiang@yongqiang:~/bash_work$ ls -l
total 0
-rwxr-xr-x 1 yongqiang yongqiang 106 May 13 20:11 bash_script.sh
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ touch "yong qiang.txt"
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ ls -l
total 0
-rwxr-xr-x 1 yongqiang yongqiang 106 May 13 20:11 bash_script.sh
-rw-r--r-- 1 yongqiang yongqiang 0 May 13 21:27 'yong qiang.txt'
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ ls -l "yong qiang.txt"
-rw-r--r-- 1 yongqiang yongqiang 0 May 13 21:27 'yong qiang.txt'
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ ls -l 'yong qiang.txt'
-rw-r--r-- 1 yongqiang yongqiang 0 May 13 21:27 'yong qiang.txt'
(base) yongqiang@yongqiang:~/bash_work$
3. 反引号 (`
)
MARKDOWN 编辑反引号 (`
)
MARKDOWN 编辑反引号 (`` ` ``)
调用命令或者将命令的输出赋予变量,则必须使用反引号包含命令,这样命令才会执行。反引号的作用和 $(命令) 是一样的,反引号容易和单引号混淆,推荐使用 $(命令) 的方式调用命令。
(base) yongqiang@yongqiang:~/bash_work$ echo ls
ls
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo 'ls'
ls
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "ls"
ls
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo `ls`
bash_script.sh yongqiang.txt
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '`ls`'
`ls`
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "`ls`"
bash_script.sh
yongqiang.txt
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $(ls)
bash_script.sh yongqiang.txt
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "$(ls)"
bash_script.sh
yongqiang.txt
(base) yongqiang@yongqiang:~/bash_work$
反引号 (`
) 括起来的命令又被单引号 ('
) 括起来,则命令不会执行,被当成普通字符输出。
反引号 (`
) 括起来的命令又被双引号 ( "
) 括起来,则命令会执行。
(base) yongqiang@yongqiang:~/bash_work$ echo date
date
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo 'date'
date
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "date"
date
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo `date`
Sun May 15 10:00:31 CST 2022
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '`date`'
`date`
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "`date`"
Sun May 15 10:00:48 CST 2022
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $(date)
Sun May 15 10:28:34 CST 2022
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "$(date)"
Sun May 15 10:34:07 CST 2022
(base) yongqiang@yongqiang:~/bash_work$
# 调用命令 echo `cal` 和 echo $(cal) 输出单行
(base) yongqiang@yongqiang:~/bash_work$ echo `cal`
May 2022 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $(cal)
May 2022 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
(base) yongqiang@yongqiang:~/bash_work$
# 调用命令 echo "$(cal)" 输出原始格式
(base) yongqiang@yongqiang:~/bash_work$ echo "$(cal)"
May 2022
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
(base) yongqiang@yongqiang:~/bash_work$
4. 单引号 ('
) – 双引号 ("
)
对于普通字符串或文本,单引号和双引号的作用相同。
(base) yongqiang@yongqiang:~/bash_work$ echo "yongqiang"
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo 'yongqiang'
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
当打印已定义的变量时,需要使用双引号,使用单引号将被视为普通字符。
(base) yongqiang@yongqiang:~/bash_work$ NAME="yongqiang"
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $NAME
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '$NAME'
$NAME
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "$NAME"
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ NAME='yongqiang'
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo $NAME
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo '$NAME'
$NAME
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo "$NAME"
yongqiang
(base) yongqiang@yongqiang:~/bash_work$
单引号 ( '
) 和双引号 ( "
) 可以保存多个连续的空格。
(base) yongqiang@yongqiang:~/bash_work$ echo "yong qiang"
yong qiang
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ echo 'yong qiang'
yong qiang
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ cat ./bash_script.sh
#!/bin/bash
echo "start"
echo
echo "Hello, world!"
NAME="yongqiang!"
echo $NAME
echo
echo "111!"
COMMENT="Hello, $NAME"
echo $COMMENT
echo
echo "222!"
COMMENT='Hello, $NAME'
echo $COMMENT
echo "Hello, world!"
echo
echo "end"
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ ./bash_script.sh
start
Hello, world!
yongqiang!
111!
Hello, yongqiang!
222!
Hello, $NAME
Hello, world!
end
(base) yongqiang@yongqiang:~/bash_work$
References
[1] Yongqiang Cheng, Linux Shell – 单引号 ( ‘ )、双引号 ( “ ) 和反引号 ( ` )
[2] Bash 教程总览, https://www.w3cschool.cn/bashshell/
[3] Bash 脚本教程, https://wangdoc.com/bash/index.html
今天的文章描述linux shell中单引号双引号及不加引号的简单区别_shell脚本双引号转义分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/82043.html