将命令结果放在环境变量中

时间:2015-04-16 09:08:47

标签: bash shell centos environment-variables

我想将命令date +"%Y.%m.%d" -d "yesterday"的结果放在环境变量中。

我尝试export YESTERDAY_DATE=date +"%Y.%m.%d" -d "yesterday",但收到错误,说明标识符不正确。

我怎么能这样做?

2 个答案:

答案 0 :(得分:3)

export YESTERDAY_DATE=`date +"%Y.%m.%d" -d "yesterday"`

答案 1 :(得分:3)

或者,您可以使用以下语法:

YESTERDAY_DATE=$(date +"%Y.%m.%d" -d "yesterday")

另见:

What's the difference between $(command) and `command` in shell programming?

编辑:缺少评论上述解决方案的能力:这就是为什么我更喜欢$()而不是` - 它的复制/可粘贴(某些系统有反引号问题......)

相关问题