当我们将命令放在美元符号和括号内时,shell 中的含义是什么:$(command) [英] What does it mean in shell when we put a command inside dollar sign and parentheses: $(command)

查看:37
本文介绍了当我们将命令放在美元符号和括号内时,shell 中的含义是什么:$(command)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解 shell 中的以下代码行.它用于获取当前工作目录.我知道 $(variable) 名称返回变量名称内的值,但是 $(command) 应该返回什么?执行命令后是否返回值?在这种情况下,我们可以使用 ` 来执行命令.

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.

CWD="$(cd "$(dirname $0)"; pwd)"

在不同版本的shell中也可以从以下代码行中获取相同的输出

Same output can be taken from the following line of code also in different version of shell

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

我无法理解 $(cd..$(dirname.

I am unable to understand the meaning of $(cd.. and $(dirname.

谁能帮我弄清楚这个命令是如何执行的?

Could anybody help me to figure out how this command get executed?

推荐答案

$ 的使用,如 ${HOME} 给出了 HOME 的值.像 $(echo foo) 一样使用 $ 意味着在子shell 中运行括号内的任何内容并将其作为值返回.在我的例子中,你会得到 foo 因为 echo 会将 foo 写入标准输出

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

这篇关于当我们将命令放在美元符号和括号内时,shell 中的含义是什么:$(command)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆