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

发布于 2025-01-07 20:34:43 字数 460 浏览 4 评论 0原文

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

我总是这样做:

echo "Hello, `whoami`."

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

echo "Hello, $(whoami)."

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

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

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

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

What's the preferred way to do command substitution in 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 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

困倦 2025-01-14 20:34:43

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

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

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

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

对于 POSIX 表单,您不需要这样做。

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

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

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

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

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

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

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

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

为了使 shell 脚本易于理解,我个人更喜欢在任何一个脚本中坚持使用一种形式或另一种形式,除非有充分的技术原因需要这样做。此外,我更喜欢 POSIX 形式而不是旧的形式;再次强调,除非有充分的技术理由不这样做。

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

-B。

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.

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.

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

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.

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.

GGG, what text editor are you using???

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.

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.

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.

-B.

江城子 2025-01-14 20:34:43

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


值得一提的是,您应该转义反引号来嵌套命令:

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

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

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


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

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

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

倒数 2025-01-14 20:34:43

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

$() 形式看起来更容易一些,尤其是。经过几关的逃脱之后。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文