简体   繁体   English

“$(变量)”和“$(VARIABLE)”之间有什么区别?

[英]What's the difference between “$(variable)” and “$(VARIABLE)”

What's the difference? 有什么不同? And why these are working: 为什么这些工作:

echo $LOGNAME       #prints username
echo "$(logname)"   #prints username

but this isn't: 但这不是:

echo "$(LOGNAME)"   #prints command not found.

logname is a command. logname是一个命令。
LOGNAME is a variable. LOGNAME是一个变量。

$(logname) works because logname command exists. $(logname)有效,因为存在logname命令。
$(LOGNAME) tries to run the command LOGNAME which does not exist. $(LOGNAME)尝试运行不存在的命令LOGNAME

Read the following useful guide 阅读以下有用指南

$LOGNAME is a variable. $LOGNAME是一个变量。 logname is a command. logname是一个命令。 When you do 当你这样做

echo $LOGNAME

you are echoing the variable, whereas when you do 你正在回应变量,而当你这样做

echo "$(logname)"

you are echoing the result of executing the command. 你正在回应执行命令的结果。 It happens to be the case that the output is the same. 恰好是输出相同的情况。

If you do env | grep LOGNAME 如果你做env | grep LOGNAME env | grep LOGNAME , you will see that $LOGNAME is an environment variable and if you do which logname you will see the path to the executable. env | grep LOGNAME ,您将看到$LOGNAME是一个环境变量,如果您执行which logname您将看到可执行文件的路径。 However, if you do which LOGNAME , you will see that there is no output. 但是,如果您执行which LOGNAME ,您将看到没有输出。 echo $? shows that the exit status of the command is 1, which means that no executable could be found. 显示该命令的退出状态为1,这意味着找不到可执行文件。

Coincidentally, you can do the same thing with $PWD and pwd . 巧合的是,你可以用$PWDpwd做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM