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

 更新时间:2024年11月01日 10:36:10   作者:黑风风  
在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单引号和双引号内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Shell iptales防火墙设置的方法步骤

    Shell iptales防火墙设置的方法步骤

    本文主要介绍了Shell iptales防火墙设置的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • shell 脚本安装PHP扩展的简单方法

    shell 脚本安装PHP扩展的简单方法

    下面小编就为大家带来一篇shell 脚本安装PHP扩展的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • Linux查看GPU信息和使用情况的实现命令

    Linux查看GPU信息和使用情况的实现命令

    这篇文章主要介绍了Linux查看GPU信息和使用情况的实现命令,文中通过示例代码和图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2023-12-12
  • shell 判断字符串是否存在数组中的实现示例

    shell 判断字符串是否存在数组中的实现示例

    这篇文章主要介绍了shell 判断字符串是否存在数组中的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • 一天一个shell命令 linux好管家--磁盘--df命令详解

    一天一个shell命令 linux好管家--磁盘--df命令详解

    这篇文章主要介绍了一天一个shell命令 linux好管家--磁盘--df命令详解 ,需要的朋友可以参考下
    2016-06-06
  • Shell脚本中管道的几种使用实例讲解

    Shell脚本中管道的几种使用实例讲解

    今天小编就为大家分享一篇关于Shell脚本中管道的几种使用实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • shell数组操作简明总结

    shell数组操作简明总结

    这篇文章主要介绍了shell数组操作总结,这里包括数组的声明、访问、复制、计算、删除、替换等操作,需要的朋友可以参考下
    2014-05-05
  • Linux 命令每天必学(34)之du命令

    Linux 命令每天必学(34)之du命令

    Linux du命令是查看使用空间的,本文给大家介绍linux du命令的参数,格式,功能等相关资料,对linux du命令感兴趣的朋友一起学习吧
    2015-11-11
  • Linux查看日志文件写入速度的4种方法详解

    Linux查看日志文件写入速度的4种方法详解

    有时,我们需要查看某个文件的增长速度,如日志文件,以此来感受系统的负载情况,因为一般情况下,日志写入越快,说明系统负载越重。本文总结了Linux中查看日志增长速度的几种方法,需要的可以参考一下
    2022-06-06
  • 浅谈shell循环中变量的作用域问题

    浅谈shell循环中变量的作用域问题

    今天小编就为大家分享一篇浅谈shell循环中变量的作用域问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06

最新评论