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

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

问题描述

我有两个问题,可以帮助理解它们.

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

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

  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++));做回声 $i;完成并且它工作正常那么为什么我不能将它用作 while ((i=0;i<10;i++));做回声 $i;完成?两者的执行周期有何不同?

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` 的现代同义词,代表命令替换;这意味着运行 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}textvar,后跟 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 循环需要一个参数,它应该评估为真或假(或者实际上是多个,检查最后一个的真值——感谢乔纳森莱夫勒指出这一点);当它为假时,不再执行循环.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 shell 只有

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天全站免登陆