linux shell

关注公众号 jb51net

关闭
首页 > 脚本专栏 > linux shell > Shell单引号和双引号

Bash Shell中单引号和双引号的区别小结

作者:黑风风

在Bash中,单引号和双引号都能定义字符串,但它们处理变量扩展、特殊字符的方式不同,本文就来介绍一下Bash Shell中单引号和双引号的区别小结,感兴趣的可以了解一下

在Bash Shell中,单引号(')和双引号(")都用于定义字符串,但它们之间有一些重要的区别。这些区别影响字符串的解释方式,变量的扩展,以及特殊字符的处理。

单引号

字面值:在单引号中的所有字符都会被视为字面值,即它们的特殊含义不会被解释。例如,$用于变量扩展,但在单引号中它只会被视为普通的美元符号。

echo '$HOME'

上面的命令将输出$HOME,而不是展开为环境变量HOME的值。

不允许嵌套单引号:单引号内不能包含其他单引号,即使它们被转义。要在单引号字符串中包含单引号,你需要使用一种特殊的语法:

echo 'It'\''s a test'

这将输出It's a test

单引号 (') 用于保留字符的字面含义,特殊字符在单引号里面,都会变为普通字符,例如 *$、反斜杠 (\) 等。

(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$

双引号

变量扩展:双引号中的变量会被扩展,即$var会被替换为变量var的值。

name="John"
echo "Hello, $name"

上面的命令将输出Hello, John

特殊字符解释:双引号中的特殊字符,如\n(换行)和\t(制表符),会被解释。

echo "Line 1\nLine 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$

单引号 (') - 双引号 (")

对于普通字符串或文本,单引号和双引号的作用相同。

(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$

到此这篇关于Bash Shell中单引号和双引号的区别小结的文章就介绍到这了,更多相关Shell单引号和双引号内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文