这是什么意思壳当我们把一个命令美元符号和括号内:$(命令) [英] What does it mean in shell when we put a command inside dollar sign and parentheses: $(command)

查看:322
本文介绍了这是什么意思壳当我们把一个命令美元符号和括号内:$(命令)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解外壳code的下面一行。它是用来获取当前的工作目录。据我所知, $(变量)名称返回变量名中的数值,但什么是 $(命令)应该返回?是否执行命令后返回值?在这种情况下,我们可以使用`来执行命令。

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)"

相同的输出可以从下面的行code的采取也可在不同版本的外壳

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 .. $(目录名

任何人可以帮我找出这个命令得到执行的如何?

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

推荐答案

的用法 $ $ {HOME} 给HOME的值。的用法 $ $(回声富)表示运行无论是在括号内的一个子shell并返回,作为价值。在我的例子,你会得到,因为回声会写到标准输出

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

这篇关于这是什么意思壳当我们把一个命令美元符号和括号内:$(命令)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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