为什么回荡行号? [英] Why echo a backticks-row?

查看:102
本文介绍了为什么回荡行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了项目中的一些代码,在一个Bash脚本中,它们在反引号行上使用 echo ,例如:

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.

那么有什么区别?为什么要使用 echo

So what's the difference? Why use echo?

将其与> 似乎更糟:

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


推荐答案

在当前形式下看起来并不是特别有用,但是将 echo 与(未引号)反引号一起使用的副作用是单词之间的空格丢失了。这是由于单词拆分的缘故-命令输出中的每个单词都被视为 echo 的单独参数,该参数仅将它们输出并用单个空格分隔。例如:

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.

这篇关于为什么回荡行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆