我正在编写一个shell脚本,其中设置了几个变量,这些变量的值是命令的输出.
I am writing a shell script where I am setting few variables, whose value is the output of commands.
我得到的错误是:
$ $tag_name="proddeploy-$(date +"%Y%m%d_%H%M")" -bash: =proddeploy-20141003_0500: command not found现在,我确实阅读了其他类似问题,并基于它,我尝试了各种方法:
now, I did read other similar questions and based on it, I tried various things:
$ $deploy_date=date +"%Y%m%d_%H%M" bash: =date: command not found $ $tag_name="proddeploy-$deploy_date" bash: proddeploy- command not found尝试使用反引号
$ $tag_name=`proddeploy-$(date +"%Y%m%d_%H%M")` bash: proddeploy-20141003_1734: command not found bash: =: command not found尝试使用$()
$ $tag_name=$(proddeploy-$(date +"%Y%m%d_%H%M")) bash: proddeploy-20141003_1735: command not found bash: =: command not found但是在每种情况下,命令输出都会执行.如何使其停止执行命令输出并仅将其存储为变量?我需要它来处理ZSH和BASH.
But in every case the command output is getting executed. how do I make it to stop executing command output and just store as a variable? I need this to work on ZSH and BASH.
推荐答案您可以使用var=string或var=$(command)定义变量.
You define variables with var=string or var=$(command).
因此,您必须删除前导$和=周围的所有其他符号:
So you have to remove the leading $ and any other signs around =:
tag_name="proddeploy-$(date +"%Y%m%d_%H%M")" deploy_date=$(date +"%Y%m%d_%H%M") ^^ ^来自命令替换:
第二种形式`COMMAND`对于Bash或多或少已经过时,因为它 嵌套时遇到了一些麻烦(内部"反引号需要转义) 和转义字符.使用$(COMMAND),它也是POSIX!
The second form `COMMAND` is more or less obsolete for Bash, since it has some trouble with nesting ("inner" backticks need to be escaped) and escaping characters. Use $(COMMAND), it's also POSIX!
此外,$()允许您嵌套,这可能很方便.
Also, $() allows you to nest, which may be handy.
更多推荐
设置变量时找不到bash命令
发布评论