为什么“echo -e a\ b\ c”会解释 '\' 转义,而“echo -e $(cat file)”却不会?
Why “echo -e a\ b\ c” interprets '\' escapes but “echo -e $(cat file)” does not?
在linux命令行中,可以使用语法
command1 $(command2)
将 command2 的输出用作参数
对于命令 1。例如,如果我有一个包含内容的文件 test.txt
this\ is\ a\ test
,我可以用(在这种情况下是废话)
echo $(cat test.txt)
回显 cat test.txt
的输出。但是,这解释了转义序列
以一种奇怪的方式:命令的输出
echo -e this\ is\ a\ test
是 this is a test
,而输出
echo -e $(cat test.txt)
是 this\ is\ a\ test
。转义序列不是
如此解读。有谁知道这是为什么吗?
用set -x
看区别:
$ cat file
this\ is\ a\ test
$ set -x
$ echo -e this\ is\ a\ test
+ echo -e 'this is a test'
this is a test
$ echo -e 'this\ is\ a\ test'
+ echo -e 'this\ is\ a\ test'
this\ is\ a\ test
$ echo -e $(cat file)
++ cat file
+ echo -e 'this\' 'is\' 'a\' test
this\ is\ a\ test
在linux命令行中,可以使用语法
command1 $(command2)
将 command2 的输出用作参数
对于命令 1。例如,如果我有一个包含内容的文件 test.txt
this\ is\ a\ test
,我可以用(在这种情况下是废话)
echo $(cat test.txt)
回显 cat test.txt
的输出。但是,这解释了转义序列
以一种奇怪的方式:命令的输出
echo -e this\ is\ a\ test
是 this is a test
,而输出
echo -e $(cat test.txt)
是 this\ is\ a\ test
。转义序列不是
如此解读。有谁知道这是为什么吗?
用set -x
看区别:
$ cat file
this\ is\ a\ test
$ set -x
$ echo -e this\ is\ a\ test
+ echo -e 'this is a test'
this is a test
$ echo -e 'this\ is\ a\ test'
+ echo -e 'this\ is\ a\ test'
this\ is\ a\ test
$ echo -e $(cat file)
++ cat file
+ echo -e 'this\' 'is\' 'a\' test
this\ is\ a\ test