bash 内联命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8663595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:25:17  来源:igfitidea点击:

bash for inline command

bashcommand

提问by dale

I have to run commands, in a bash script, and within this bash script I have to run other commands. I am running CentOS.

我必须在 bash 脚本中运行命令,并且在这个 bash 脚本中我必须运行其他命令。我正在运行 CentOS。

I found 2 ways to do this on blogs and manuals:

我在博客和手册上找到了两种方法:

1) using the ticks or accent char

1) 使用刻度或重音字符

command `sub command`

or

或者

2) using the dollar sign and parentheses

2) 使用美元符号和括号

command $(sub command)

What is the difference between the 2 and which one is preferable to use?

2 和哪个更适合使用之间有什么区别?

回答by holygeek

There's no difference except in "nestability":

除了“可嵌套性”之外没有区别:

The $()is nestable:

$()是嵌套:

$ echo $(echo "hi" $(echo "there"))

while the `` is not.

而``不是。

回答by Gordon Davisson

Others have pointed out the difference in syntax (basically, $()is slightly cleaner wrt nesting and escapes), but nobody's mentioned what I consider the more important difference: $()is much easier to read. It doesn't look like single-quotes (which mean something totally different), and the opening and closing delimiters are different, making it easier to visually distinguish its contents.

其他人指出了语法上的差异(基本上,$()嵌套和转义稍微干净一点),但没有人提到我认为更重要的区别:$()更容易阅读。它看起来不像单引号(这意味着完全不同的东西),并且开始和结束分隔符不同,更容易在视觉上区分其内容。

For your own scripts, this may not be really critical; code readability is good, but functionality is more important. But for anyone writing tutorials, sample code, stackoverflow answers, etc, readability is much more important. People willtype single-quotes instead of backquotes when typing in examples, etc, and then get confused when it doesn't work as expected.

对于您自己的脚本,这可能并不重要;代码可读性好,但功能更重要。但是对于任何编写教程、示例代码、stackoverflow 答案等的人来说,可读性更为重要。人们在输入示例等时输入单引号而不是反引号,然后当它没有按预期工作时会感到困惑。

So for everyone writing examples on stackoverflow: please save your readers some trouble, and always use the $()form.

因此,对于在 stackoverflow 上编写示例的每个人:请为您的读者省去一些麻烦,并始终使用$()表格。

回答by duskwuff -inactive-

$(...)and backticks are very similar. The only difference between the two is some details of what special characters are substituted in them; the manual explains better than I could:

$(...)和反引号非常相似。两者之间的唯一区别是其中替换了哪些特殊字符的一些细节;手册比我能解释的更好:

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or . The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

当使用旧式反引号替换形式时,反斜杠保留其字面意义,除非后跟 $、` 或 。前面没有反斜杠的第一个反引号终止命令替换。使用 $(command) 形式时,括号之间的所有字符组成命令;没有被特殊对待。

This makes it a bit easier to nest $(...), for instance. Besides that, though, there's no difference.

例如,这使得嵌套更容易$(...)一些。不过除此之外,没有什么区别。

回答by another.anon.coward

Quoting from http://tldp.org/LDP/abs/html/commandsub.html:

引自http://tldp.org/LDP/abs/html/commandsub.html

  1. The $(...) form of command substitution treats a double backslash in a different way than `...`.
  2. The $(...) form of command substitution permits nesting
  1. $(...) 形式的命令替换以不同于 `...` 的方式处理双反斜杠。
  2. $(...) 形式的命令替换允许嵌套

Hope this helps!

希望这可以帮助!

回答by Grasshopper

I must agree with Gordon in the fact that $() is way cleaner. I struggled for 2 hours editing my .bash_profile to add homebrew bash completion only to find that I should've been using ` and not '.

我必须同意 Gordon 的观点,即 $() 更干净。我努力了 2 个小时来编辑我的 .bash_profile 以添加自制的 bash 完成,结果发现我应该一直使用“而不是”。