Bash中$ {}和$()之间的区别 [英] Difference between ${} and $() in Bash

查看:108
本文介绍了Bash中$ {}和$()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题,可能需要一些帮助来理解它们.

I have two questions and could use some help understanding them.

  1. ${}$()有什么区别?我了解() 表示在单独的外壳中运行命令,并放置$表示传递 变量的值.有人可以帮助我理解 这?如果我错了,请纠正我.

  1. What is the difference between ${} and $()? I understand that () means running command in separate shell and placing $ means passing the value to variable. Can someone help me in understanding this? Please correct me if I am wrong.

如果我们可以使用for ((i=0;i<10;i++)); do echo $i; done并且它可以正常工作,那为什么我不能将其用作while ((i=0;i<10;i++)); do echo $i; done?两者的执行周期有何不同?

If we can use for ((i=0;i<10;i++)); do echo $i; done and it works fine then why can't I use it as while ((i=0;i<10;i++)); do echo $i; done? What is the difference in execution cycle for both?

推荐答案

语法是令牌级别的,因此美元符号的含义取决于其所在的令牌.表达式$(command)代表命令替换;这意味着运行command并将其输出放在此处.所以

The syntax is token-level, so the meaning of the dollar sign depends on the token it's in. The expression $(command) is a modern synonym for `command` which stands for command substitution; it means run command and put its output here. So

echo "Today is $(date). A fine day."

将运行date命令,并将其输出包含在echo的参数中.括号与在子shell中运行命令的语法无关,尽管它们有一些共同点(命令替换也在单独的子shell中运行).

will run the date command and include its output in the argument to echo. The parentheses are unrelated to the syntax for running a command in a subshell, although they have something in common (the command substitution also runs in a separate subshell).

相反,${variable}只是消除歧义的机制,因此当您表示变量var的内容时,可以说${var}text,后跟text(而不是$vartext,这意味着内容vartext).

By contrast, ${variable} is just a disambiguation mechanism, so you can say ${var}text when you mean the contents of the variable var, followed by text (as opposed to $vartext which means the contents of the variable vartext).

while循环需要一个参数,该参数的值应该为true或false(或者实际上是多个,在其中检查最后一个的真值-谢谢J​​onathan Leffler指出了这一点);如果为假,则不再执行循环. for循环遍历一个项目列表,然后依次将每个项目绑定到一个循环变量.您所指的语法是一种在一定范围的算术值上表达循环的方式(而不是一般性的方式).

The while loop expects a single argument which should evaluate to true or false (or actually multiple, where the last one's truth value is examined -- thanks Jonathan Leffler for pointing this out); when it's false, the loop is no longer executed. The for loop iterates over a list of items and binds each to a loop variable in turn; the syntax you refer to is one (rather generalized) way to express a loop over a range of arithmetic values.

这样的for循环可以改写为while循环.表达式

A for loop like that can be rephrased as a while loop. The expression

for ((init; check; step)); do
    body
done

等同于

init
while check; do
    body
    step
done

为了清晰起见,将所有循环控制都放在一个位置很有意义;但是正如您所看到的那样,for循环比while循环做的要多得多.

It makes sense to keep all the loop control in one place for legibility; but as you can see when it's expressed like this, the for loop does quite a bit more than the while loop.

当然,此语法是特定于Bash的;经典的Bourne外壳只有

Of course, this syntax is Bash-specific; classic Bourne shell only has

for variable in token1 token2 ...; do


(稍微优雅一点,只要您确定自变量字符串不包含任何%格式代码,就可以在第一个示例中避免使用echo:


(Somewhat more elegantly, you could avoid the echo in the first example as long as you are sure that your argument string doesn't contain any % format codes:

date +'Today is %c. A fine day.'

避免可能的过程是一个重要的考虑因素,即使在这个孤立的示例中并没有多大区别.)

Avoiding a process where you can is an important consideration, even though it doesn't make a lot of difference in this isolated example.)

这篇关于Bash中$ {}和$()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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