巴什反引号VS牙套 [英] Bash Backticks vs Braces
问题描述
当我去接<一个href=\"http://stackoverflow.com/questions/22693415/cpu-usage-from-linux-then-using-it-in-a-arithmetic-ex$p$pssion\">this问题,我会使用 $ {}
符号,因为我已经看到了这里,它是preferable以反引号这么多次。
然而,当我试图
joulesFinal = $ {回声$ joules2 \\ * $ CPU |公元前}
我得到的消息
-bash:$ {回声$焦耳* $ CPU | BC}:替代不好
但
joulesFinal =`回声$ joules2 \\ * $ CPU | bc`
正常工作。那么,什么其他的变化,我需要做什么呢?
的``
被称为命令替换,相当于 $()
(括号),而你正在使用 $ {}
(大括号)。
因此,这些都是平等的,意思是国米preT置于其内的命令:
joulesFinal =`回声$ joules2 \\ * $ CPU | bc`
joulesFinal = $(回声$ joules2 \\ * $ CPU | BC)
^^
(代替{(代替{
在 $ {}
前pressions用于变量替换。
从男人庆典
:
命令替换可让一个命令的输出,以取代
命令名称。有两种形式:$(命令)
要么
`command`
块引用>此外,
``
更难以处理,你不能嵌套他们的例子。见下面的评论,也为什么$(...)preferred超过...
(反引号)?。When I went to answer this question, I was going to use the
${}
notation, as I've seen so many times on here that it's preferable to backticks. However, when I triedjoulesFinal=${echo $joules2 \* $cpu | bc}
I got the message
-bash: ${echo $joules * $cpu | bc}: bad substitution
but
joulesFinal=`echo $joules2 \* $cpu | bc`
works fine. So what other changes do I need to make?
解决方案The
``
is called Command Substitution and is equivalent to$()
(parenthesis), while you are using${}
(curly braces).So these are equal and mean "interpret the command placed inside":
joulesFinal=`echo $joules2 \* $cpu | bc` joulesFinal=$(echo $joules2 \* $cpu | bc) ^ ^ ( instead of { ( instead of {
While
${}
expressions are used for variable substitution.
From
man bash
:Command substitution allows the output of a command to replace the command name. There are two forms:
$(command) or `command`
Also,
``
are more difficult to handle, you cannot nest them for example. See comments below and also Why is $(...) preferred over...
(backticks)?.这篇关于巴什反引号VS牙套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!