设置变量时找不到bash命令

编程入门 行业动态 更新时间:2024-10-10 05:25:02
本文介绍了设置变量时找不到bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个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命令

本文发布于:2023-11-09 03:57:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   变量   命令   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!