如何将 Bash 命令的输出分配给变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2314750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to assign the output of a Bash command to a variable?
提问by Zenet
I have a problem putting the content of pwd
command into a shell variable that I'll use later.
我在将pwd
命令的内容放入稍后将使用的 shell 变量时遇到问题。
Here is my shell code (the loop doesn't stop):
这是我的 shell 代码(循环不会停止):
#!/bin/bash
pwd= `pwd`
until [ $pwd = "/" ]
do
echo $pwd
ls && cd .. && ls
$pwd= `pwd`
done
Could you spot my mistake, please?
你能发现我的错误吗?
回答by John Weldon
Try:
尝试:
pwd=`pwd`
or
或者
pwd=$(pwd)
Notice no spaces after the equals sign.
注意等号后没有空格。
Also as Mr. Weiss points out; you don't assign to $pwd
, you assign to pwd
.
同样正如魏斯先生指出的那样;你不分配给$pwd
,你分配给pwd
。
回答by Johannes Weiss
In shell you assign to a variable without the dollar-sign:
在 shell 中,您分配给一个没有美元符号的变量:
TEST=`pwd`
echo $TEST
that's better (and can be nested) but is not as portable as the backtics:
这更好(并且可以嵌套)但不如 backtics 便携:
TEST=$(pwd)
echo $TEST
Always remember: the dollar-sign is only used when reading a variable.
永远记住:美元符号仅在读取变量时使用。
回答by Gilles 'SO- stop being evil'
In this specific case, note that bash has a variable called PWD
that contains the current directory: $PWD
is equivalent to `pwd`
. (So do other shells, this is a standard feature.) So you can write your script like this:
在这种特定情况下,请注意 bash 有一个名为的变量PWD
,其中包含当前目录:$PWD
相当于`pwd`
. (其他 shell 也是如此,这是一个标准特性。)所以你可以像这样编写你的脚本:
#!/bin/bash
until [ "$PWD" = "/" ]; do
echo "$PWD"
ls && cd .. && ls
done
Note the use of double quotes around the variable references. They are necessary if the variable (here, the current directory) contains whitespace or wildcards (\[?*
), because the shell splits the result of variable expansions into words and performs globbing on these words. Always double-quote variable expansions "$foo"
and command substitutions "$(foo)"
(unless you specifically know you have not to).
请注意在变量引用周围使用双引号。如果变量(此处为当前目录)包含空格或通配符 ( \[?*
),则它们是必需的,因为 shell 将变量扩展的结果拆分为单词并对这些单词执行通配符。始终双引号变量扩展"$foo"
和命令替换"$(foo)"
(除非您明确知道您不必这样做)。
In the general case, as other answers have mentioned already:
在一般情况下,正如其他答案已经提到的:
- You can't use whitespace around the equal sign in an assignment:
var=value
, notvar = value
- The
$
means “take the value of this variable”, so you don't use it when assigning:var=value
, not.$var=value
- 您不能在赋值中的等号周围使用空格:
var=value
, notvar = value
- 的
$
意思是“利用这个变量的值”,所以分配当你不使用它:var=value
不是。$var=value
回答by Jim
You can also do way more complex commands, just to round out the examples above. So, say I want to get the number of processes running on the system and store it in the ${NUM_PROCS}variable.
您还可以执行更复杂的命令,只是为了完善上面的示例。因此,假设我想获取系统上运行的进程数并将其存储在${NUM_PROCS}变量中。
All you have to so is generate the command pipeline and stuff it's output (the process count) into the variable.
您所要做的就是生成命令管道并将其输出(进程计数)填充到变量中。
It looks something like this:
它看起来像这样:
NUM_PROCS=$(ps -e | sed 1d | wc -l)
I hope that helps add some handy information to this discussion.
我希望这有助于为这次讨论添加一些方便的信息。
回答by Nikolas Wolfe
Here's your script...
这是你的脚本...
DIR=$(pwd)
echo $DIR
while [ "$DIR" != "/" ]; do
cd ..
DIR=$(pwd)
echo $DIR
done
Note the spaces, use of quotes, and $ signs.
注意空格、引号的使用和 $ 符号。