简体   繁体   English

使用xargs和xargs -0的Bash命令

[英]Bash command using xargs and xargs -0

I just found difference between two commands: 我只是发现两个命令之间的区别:

echo sum.txt| xargs cat

This will output content of sum.txt 这将输出sum.txt的内容

echo sum.txt| xargs -0 cat

This shows error: 这显示错误:

cat: sum.txt
: No such file or directory

I know -0 will treat null bytes as delimiter. 我知道-0将空字节视为定界符。 And i think the new line starts with : is because echo command produce new lines. 我认为新行以:开头是因为echo命令会产生新行。 And doesn't produce output like: 并且不会产生如下输出:

cat: sum.txt: No such file or directory

But if echo produce new lines why the first command can succeed? 但是,如果echo产生换行符,那么第一个命令为什么可以成功? since xargs use white spaces as delimiter by default. 因为xargs默认使用空格作为定界符。

I think what is happening will become more clear if you replace cat with echo -n as an experiment. 我认为,如果您用echo -n代替cat作为实验,将会更加清楚。

echo sum.txt| xargs  echo -n
echo sum.txt | xargs -0 echo -n

In the first example, xargs breaks on the newline and discards the newline , leaving just sum.txt . 在第一个示例中, xargs在换行符处中断并丢弃换行符 ,仅sum.txt

In the second example, xargs breaks on the end of the EOF at the end of the output of the first echo , resulting in the string 'sum.txt\\n' . 在第二个示例中, xargs在第一个echo的输出结尾处的EOF结尾处中断,从而产生字符串'sum.txt\\n'

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

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