bash 当我们将命令放在美元符号和括号内时,shell 中的含义是什么:$(command)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17984958/
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
What does it mean in shell when we put a command inside dollar sign and parentheses: $(command)
提问by KItis
I just want to understand following line of code in shell. It is used to get the current working directory. I am aware that $(variable)
name return the value inside the variable name, but what is $(command)
supposed to return? Does it return the value after executing the command? In that case, we can use `
to execute the command.
我只想了解 shell 中的以下代码行。它用于获取当前工作目录。我知道$(variable)
name 返回变量 name 中的值,但是$(command)
应该返回什么?执行命令后是否返回值?在这种情况下,我们可以使用`
来执行命令。
CWD="$(cd "$(dirname DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
)"; pwd)"
Same output can be taken from the following line of code also in different version of shell
在不同版本的 shell 中也可以从以下代码行中获取相同的输出
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
I am unable to understand the meaning of $(cd..
and $(dirname
.
我无法理解的含义$(cd..
和$(dirname
。
Could anybody help me to figure out how this command get executed?
有人能帮我弄清楚这个命令是如何执行的吗?
回答by Eric Urban
Usage of the $
like ${HOME}
gives the value of HOME. Usage of the $
like $(echo foo)
means run whatever is inside the parentheses in a subshell and return that as the value. In my example, you would get foo
since echo
will write foo
to standard out
$
类似的用法${HOME}
给出了 HOME 的值。使用$
like$(echo foo)
意味着在子shell 中运行括号内的任何内容并将其作为值返回。在我的例子中,你会得到foo
因为echo
将写入foo
标准输出
回答by devnull
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
Could anybody help me to figure out how this command get executed?
##代码##有人能帮我弄清楚这个命令是如何执行的吗?
Let's look at different parts of the command. BASH_SOURCE
is a bash array variable containing source filenames. So "${BASH_SOURCE[0]}"
would return you the name of the script file.
让我们看看命令的不同部分。BASH_SOURCE
是一个包含源文件名的 bash 数组变量。所以"${BASH_SOURCE[0]}"
会返回脚本文件的名称。
dirname
is a utility provided by GNU coreutils that remove the last componentfrom the filename. Thus if you execute your script by saying bash foo
, "$( dirname "${BASH_SOURCE[0]}" )"
would return .
. If you said bash ../foo
, it'd return ..
; for bash /some/path/foo
it'd return /some/path
.
dirname
是 GNU coreutils 提供的实用程序,用于从文件名中删除最后一个组件。因此,如果您通过说bash foo
,"$( dirname "${BASH_SOURCE[0]}" )"
来执行您的脚本,将返回.
。如果你说bash ../foo
,它会回来..
;因为bash /some/path/foo
它会回来/some/path
。
Finally, the entire command "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
gets the absolute directory containing the script being invoked.
最后,整个命令"$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
获取包含正在调用的脚本的绝对目录。
$(...)
allows command substitution, i.e. allows the output of a command to replace the command itself and can be nested.
$(...)
允许命令替换,即允许命令的输出替换命令本身并且可以嵌套。