echo bash 变量作为文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18831153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:35:02  来源:igfitidea点击:

echo bash variable as text

bashvariablesecho

提问by alexj

I am trying to echo a var as text and not parse it. Everything I have tried and searched for just seems to show me how to echo the variable and not the variable text only.

我试图将 var 作为文本回显而不是解析它。我尝试和搜索的所有内容似乎都在向我展示如何回显变量,而不仅仅是变量文本。

Note the code below is just what I have as basic echo. I have tried \"$ARG1$\" etc with this line.

请注意,下面的代码正是我所拥有的基本回声。我已经用这条线尝试过 \"$ARG1$\" 等。

echo "command[runcmd]=sudo service $ARG1$ restart" >> testfile

What I want is for it to show

我想要的是让它显示

command[runcmd]=sudo service $ARG1$ restart

BUT what it ends up as is

但结果是这样

command[runcmd]=sudo service $ restart

I know this is a syntax error on my part but I can't seem to find the proper way even in the in MAN pages. Maybe I am missing something.

我知道这是我的语法错误,但即使在 MAN 页面中我似乎也找不到正确的方法。也许我错过了一些东西。

Thank you in advance!

先感谢您!

回答by glenn Hymanman

Just use single quotes: they prevent variable substitution:

只需使用单引号:它们可以防止变量替换:

echo 'command[runcmd]=sudo service $ARG1$ restart'

回答by Paul Evans

Use single quotes (') instead of double ("):

使用单引号 (') 而不是双引号 ("):

echo 'command[runcmd]=sudo service $ARG1$ restart' >> testfile