当我去回答
this question时,我打算使用$ {}表示法,因为我在这里已经看过很多次,所以最好是反叛。
但是,当我尝试
joulesFinal=${echo $joules2 \* $cpu | bc}@H_502_4@我收到了消息
-bash: ${echo $joules * $cpu | bc}: bad substitution@H_502_4@但
joulesFinal=`echo $joules2 \* $cpu | bc`@H_502_4@工作正常。那么我需要做出哪些其他改变呢?
“被称为命令替换,相当于$()(括号),而你使用$ {}(花括号)。
所以这些是平等的,意思是“解释放在里面的命令”:
joulesFinal=`echo $joules2 \* $cpu | bc` joulesFinal=$(echo $joules2 \* $cpu | bc) ^ ^ ( instead of { ) instead of }@H_502_4@$ {}表达式用于变量替换。
来自man bash:
Command substitution allows the output of a command to replace the
command name. There are two forms:06001
而且,“更难以处理,例如你不能嵌套它们。见下面的评论以及Why is $(…) preferred over
...
(backticks)?。