命令替换:反引号或美元符号/括号括起来? [英] Command substitution: backticks or dollar sign / paren enclosed?

查看:159
本文介绍了命令替换:反引号或美元符号/括号括起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是preferred方式做命令替换在bash?

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

我一直在做这样的:

echo "Hello, `whoami`."

不过,最近我经常看到它这样写的:

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

echo "Hello, $(whoami)."

什么是preferred语法,为什么?或者他们pretty可以互换?

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.

我看了这里逃脱字符在每种情况下不同的行为有点,但它不是我清楚哪些行为是preferable,或者如果它只是视情况而定。

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?

推荐答案

有几个问题/问题在这里,所以我会重复海报的文字,块引用的每个部分,其次是我的回答。

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

什么是preferred语法,为什么?或者他们pretty可以互换?

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

我要说的是, $(some_command)的形式是pferred在`some_command` 形式$ P $ 。第二种形式,使用一对后引号(以下简称`字符,也称为反引号和重音符),是做它的历史道路。第一种形式,使用美元符号和括号,是一个较新的POSIX的形式,这意味着它可能做的更标准的方式。反过来,我认为这意味着它更可能以不同的炮弹和不同的* nix实现正常工作。

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.

为preferring第一(POSIX)形式给出的另一个原因是,它更容易阅读,尤其是当命令替换嵌套。此外,与反引号的形式,反引号字符必须是反斜杠转义的嵌套(内部)命令替换。

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.

至于他们是否是可以互换的,好了,我想说的是,在一般情况下,他们是可以互换的,除了你的转义字符中提到的例外情况。但是,我不知道也不能说所有的现代炮弹和各种现代化的* nixes是否支持这两种形式。我怀疑,他们这样做,尤其是老年弹/年长* nixes。如果我是你,我就不会依赖于互换性无需先运行一对夫妇的每个表单的快捷,简单的测试,在您计划在运行完成的脚本任何shell / * nix的实现。

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.

这是不幸的,你的编辑器似乎不支持POSIX形式;也许你应该检查,看看是否有一个支持这样做的POSIX方式编辑器的更新。龙也许出手,但谁知道?或者,也许你应该甚至可以考虑尝试不同的编辑器。

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,您使用什么样的文本编辑器???

GGG, what text editor are you using???

我读到这里转义字符在每种情况下不同行为了一点,但它不是我清楚哪些行为是preferable,或者如果它只是视情况而定。

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脚本可以理解的,我个人preFER整个任何一个脚本坚持一个形式或其他,除非有很好的技术理由不这样做。此外,我想preFER POSIX的形式比旧形式;再次,除非有一个良好的技术理由不这样做。

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.

更多关于命令替换的话题,做它的两种不同形式,我建议你向相关章节命令替换在O'Reilly出版的书经典Shell脚本,第二版,由罗宾斯和毕比。在这部分中,作者指出,对命令替换的POSIX形式的建议对所有新的发展。我在这本书中没有任何经济利益;这只是我有(和爱)shell脚本,虽然它的多为中级或高级shell脚本,并没有真正的开始shell脚本。

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。

这篇关于命令替换:反引号或美元符号/括号括起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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