linux - bash 脚本中双引号的意外行为

标签 linux bash shell unix

我创建了一个如下所示的变量:

firstAndLastLines="$(cat /etc/passwd | head -$n | cut -f5 -d':') $(cat /etc/passwd | tail -$n | cut -f5 -d':') "

如果我使用 echo $firstAndLastLines 将它打印到终端,我会得到以下输出:
root bin daemon adm lp trousers 包用来对 tcsd daemon Norbert Fogarasi 进行沙盒处理的特权分离 SSH 帐户

但是,如果我使用 echo "$firstAndLastLines",我会得到以下内容,以新行分隔:

root
bin
daemon
adm
lp 
Privilege-separated SSH
Account used by the trousers package to sandbox the tcsd daemon

Norbert Fogarasi`

我想知道,为什么会这样?它不必是相同的? `

最佳答案

bash ma​​n 中的解释:

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \,

关于linux - bash 脚本中双引号的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44778677/

相关文章:

c - HXmap_get 未使用 key 返回正确的数据

bash - 使用 AWK 递归查找

Bash tail 命令和多个管道

python - 采购 Anaconda 激活脚本与将 Anaconda bin 目录添加到 PATH

linux - 从宏运行操作系统命令

linux - 使用 ssh 连接时 git clone 失败

c - Linux 中的非阻塞连接

bash - 如何在 Macbook 上为 oh-my-zsh 更改我的 $PS1?

linux - 仅返回匹配模式之后的行部分

linux - BASH:如何对管道中的数字执行算术运算