Why echo a backticks-row?

爱⌒轻易说出口 提交于 2019-12-20 04:57:11

问题


I "inherited" some code in a project, and in one of the Bash scripts, they use echo on a backticks-row, like:

#!/bin/bash
echo `/path/command argument`

What's the difference between that and just running the command itself?

#!/bin/bash
/path/command argument

Both send the command's output to the script's stdout.

So what's the difference? Why use echo?

To combine it with a > seems even worse:

#!/bin/bash
echo `/path/command argument > /var/log/exemple.log`

回答1:


It doesn't look particularly useful in its current form but one side-effect of using echo with (unquoted) backticks is that whitespace between words is lost. This is because of word splitting - every word in the output of the command is treated as a separate argument to echo, which just outputs them separated by a single space. For example:

$ echo "a   b c"
a   b c
$ echo `echo "a   b c"`
a b c

Note that this applies to all types of whitespace such as tab characters and newlines.

I'm not sure why you'd want to do this deliberately! Personally, I'd be tempted to just run the command normally.



来源:https://stackoverflow.com/questions/36039511/why-echo-a-backticks-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!