bash 如何在bash脚本中打印文字字符串“$1”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16445292/
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
How to print literal string "$1" in bash script?
提问by Ziyaddin Sadigov
I want to print string called "$1". But when I do this with echo it prints string which equals to "$1" variable. How can I print "$1" just like string?
我想打印名为“$1”的字符串。但是当我用 echo 执行此操作时,它会打印等于“$1”变量的字符串。如何像字符串一样打印“$ 1”?
for example:
例如:
set -- "output" # this sets to be "output"
echo # ==> output
But I want this:
但我想要这个:
echo # ==>
回答by fedorqui 'SO stop harming'
You have to escape the $
to have it shown:
你必须转义$
它才能显示它:
$ echo "$1"
or, as noted by JeremyP in comments, just use single quotes so that the value of $1
does not get expanded:
或者,正如 JeremyP 在评论中所指出的,只使用单引号,这样 的值$1
就不会被扩展:
$ echo ''
回答by Carlos Campderrós
You need to either:
您需要:
- Enclose the variable in SINGLE quotes:
echo '$1'
- Escape the
$
sign:echo "\$1"
- 将变量括在单引号中:
echo '$1'
- 逃离
$
标志:echo "\$1"