简体   繁体   English

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

[英]Unexpected behaviour of double quotes in bash script

I've created a variable which looks like this: 我创建了一个看起来像这样的变量:

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

If I print it to the terminal, using echo $firstAndLastLines , I get the following output: 如果使用echo $firstAndLastLines将其打印到终端, echo $firstAndLastLines得到以下输出:
root bin daemon adm lp Privilege-separated SSH Account used by the trousers package to sandbox the tcsd daemon Norbert Fogarasi

But, if I use echo "$firstAndLastLines" , I get the following, separated by new lines: 但是,如果我使用echo "$firstAndLastLines"echo "$firstAndLastLines"得到以下内容,并用新行分隔:

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

Norbert Fogarasi`

I wondered, why is this happening? 我想知道为什么会这样吗? It does not have to be the same? 它不必相同吗? ` `

Explanation in bash man : bash man中的解释:

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \\, 双引号引起来字符将保留引号中所有字符的文字值 ,但$,`,\\,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM