bash 命令替换:反引号或美元符号/括号括起来?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9405478/
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:41:09  来源:igfitidea点击:

Command substitution: backticks or dollar sign / paren enclosed?

bashshellcommandsubstitution

提问by Dagg Nabbit

What's the preferred way to do command substitution in bash?

在 bash 中进行命令替换的首选方法是什么?

I've always done it like this:

我一直是这样做的:

echo "Hello, `whoami`."

But recently, I've often seen it written like this:

但是最近,我经常看到它是这样写的:

echo "Hello, $(whoami)."

What's the preferred syntax, and why? Or are they pretty much interchangeable?

什么是首选语法,为什么?或者它们几乎可以互换?

I tend to favor the first, simply because my text editor seems to know what it is, and does syntax highlighting appropriately.

我倾向于第一个,因为我的文本编辑器似乎知道它是什么,并且适当地突出显示语法。

I read herethat escaped characters act a bit differently in each case, but it's not clear to me which behavior is preferable, or if it just depends on the situation.

在这里读到转义字符在每种情况下的行为都略有不同,但我不清楚哪种行为更可取,或者是否仅取决于情况。

Side question: Is it bad practice to use bothforms in one script, for example when nesting command substitutions?

附带问题:在一个脚本中使用这两种形式是不是不好的做法,例如在嵌套命令替换时?

采纳答案by Bruce Brown

There are several questions/issues here, so I'll repeat each section of the poster's text, block-quoted, and followed by my response.

这里有几个问题/问题,所以我将重复海报文本的每个部分,引用块,然后是我的回应。

What's the preferred syntax, and why? Or are they pretty much interchangeable?

什么是首选语法,为什么?或者它们几乎可以互换?

I would say that the $(some_command)form is preferred over the `some_command`form. The second form, using a pair of backquotes (the "`" character, also called a backtick and a grave accent), is the historical way of doing it. The first form, using dollar sign and parentheses, is a newer POSIX form, which means it's probably a more standard way of doing it. In turn, I'd think that that means it's more likely to work correctly with different shells and with different *nix implementations.

我会说$(some_command)形式比形式更受欢迎`some_command`。第二种形式,使用一对反引号(“`”字符,也称为反引号和重音符号),是历史上这样做的方式。第一种使用美元符号和括号的形式是较新的 POSIX 形式,这意味着它可能是一种更标准的方式。反过来,我认为这意味着它更有可能与不同的 shell 和不同的 *nix 实现一起正常工作。

Another reason given for preferring the first (POSIX) form is that it's easier to read, especially when command substitutions are nested. Plus, with the backtick form, the backtick characters have to be backslash-escaped in the nested (inner) command substitutions.

首选第一种 (POSIX) 形式的另一个原因是它更易于阅读,尤其是在嵌套命令替换时。另外,对于反引号形式,反引号字符必须在嵌套(内部)命令替换中进行反斜杠转义。

With the POSIX form, you don't need to do that.

使用 POSIX 表单,您不需要这样做。

As far as whether they're interchangeable, well, I'd say that, in general, they are interchangeable, apart from the exceptions you mentioned for escaped characters. However, I don't know and cannot say whether all modern shells and all modern *nixes support both forms. I doubt that they do, especially older shells/older *nixes. If I were you, I wouldn't depend on interchangeability without first running a couple of quick, simple tests of each form on any shell/*nix implementations that you plan to run your finished scripts on.

至于它们是否可以互换,我想说的是,总的来说,它们是可以互换的,除了你提到的转义字符的例外。但是,我不知道也不能说是否所有现代 shell 和所有现代 *nixes 都支持这两种形式。我怀疑他们这样做,尤其是较旧的 shell/较旧的 *nixes。如果我是你,我不会依赖可互换性,而不会首先在你计划运行已完成脚本的任何 shell/*nix 实现上对每种形式运行几个快速、简单的测试。

I tend to favor the first, simply because my text editor seems to know what it is, and does syntax highlighting appropriately.

我倾向于第一个,因为我的文本编辑器似乎知道它是什么,并且适当地突出显示语法。

It's unfortunate that your editor doesn't seem to support the POSIX form; maybe you should check to see if there's an update to your editor that supports the POSIX way of doing it. Long shot maybe, but who knows? Or, maybe you should even consider trying a different editor.

不幸的是,您的编辑器似乎不支持 POSIX 形式;也许您应该检查一下您的编辑器是否有支持 POSIX 方式的更新。也许是远射,但谁知道呢?或者,也许您甚至应该考虑尝试不同的编辑器。

GGG, what text editor are you using???

GGG,你用的是什么文本编辑器???

I read here that escaped characters act a bit differently in each case, but it's not clear to me which behavior is preferable, or if it just depends on the situation.

我在这里读到转义字符在每种情况下的行为都略有不同,但我不清楚哪种行为更可取,或者是否仅取决于情况。

I'd say that it depends on what you're trying to accomplish; in other words, whether you're using escaped characters along with command substitution or not.

我会说这取决于你想要完成什么;换句话说,无论您是否使用转义字符和命令替换。

Side question: Is it bad practice to use both forms in one script, for example when nesting command substitutions?

附带问题:在一个脚本中使用这两种形式是不是不好的做法,例如在嵌套命令替换时?

Well, it might make the script slightly easier to READ (typographically speaking), but harder to UNDERSTAND! Someone reading your script (or YOU, reading it six months later!) would likely wonder why you didn't just stick to one form or the other--unless you put some sort of note about why you did this in the comments. Plus, mixing both forms in one script would make that script less likely to be portable: In order for the script to work properly, the shell that's executing it has to support BOTH forms, not just one form or the other.

好吧,它可能会使脚本更容易阅读(从印刷上来说),但更难理解!阅读你的脚本的人(或者你,六个月后阅读它!)可能会想知道为什么你不只是坚持一种或另一种形式——除非你在评论中注明你为什么这样做。另外,在一个脚本中混合两种形式会降低该脚本的可移植性:为了使脚本正常工作,执行它的 shell 必须支持两种形式,而不仅仅是一种形式或另一种形式。

For making a shell script understandable, I'd personally prefer sticking to one form or the other throughout any one script, unless there's a good technical reason to do otherwise. Moreover, I'd prefer the POSIX form over the older form; again, unless there's a good technical reason to do otherwise.

为了使 shell 脚本易于理解,我个人更喜欢在任何一个脚本中坚持使用一种形式或另一种形式,除非有很好的技术理由不这样做。此外,与旧形式相比,我更喜欢 POSIX 形式;再一次,除非有很好的技术理由不这样做。

For more on the topic of command substitution, and the two different forms for doing it, I suggest you refer to the section on command substitution in the O'Reilly book "Classic Shell Scripting," second edition, by Robbins and Beebe. In that section, the authors state that the POSIX form for command substitution "is recommended for all new development." I have no financial interest in this book; it's just one I have (and love) on shell scripting, though it's more for intermediate or advanced shell scripting, and not really for beginning shell scripting.

有关命令替换主题以及执行此操作的两种不同形式的更多信息,我建议您参考 O'Reilly 书籍“Classic Shell Scripting”,第二版,Robbins 和 Beebe 中有关命令替换的部分。在该部分中,作者声明命令替换的 POSIX 形式“推荐用于所有新开发”。我对这本书没有经济利益;它只是我在 shell 脚本方面拥有(并且喜欢)的一个,尽管它更多地用于中级或高级 shell 脚本,而不是真正用于开始 shell 脚本。

-B.

-B。

回答by kev

You can read the differences from bash manual. At most case, they are interchangeable.

您可以阅读bash 手册中的差异。大多数情况下,它们是可以互换的。



One thing to mention is that you should escape backquoteto nest commands:

需要提及的一件事是,您应该转义backquote嵌套命令:

$ echo $(echo hello $(echo word))
hello word    

$ echo `echo hello \`echo word\``
hello word


回答by phs

The backticks are compatible with ancient shells, and so scripts that need to be portable (such as GNU autoconf snippets) should prefer them.

反引号与古老的 shell 兼容,因此需要可移植的脚本(例如 GNU autoconf 片段)应该更喜欢它们。

The $()form is a little easier on the eyes, esp. after a few levels of escaping.

这种$()形式在眼睛上更容易一些,尤其是。经过几级逃生。