简体   繁体   English

如何在bash脚本中打印文字字符串“$ 1”?

[英]How to print literal string “$1” in bash script?

I want to print string called "$1". 我想打印名为“$ 1”的字符串。 But when I do this with echo it prints string which equals to "$1" variable. 但是当我使用echo执行此操作时,它会输出等于“$ 1”变量的字符串。 How can I print "$1" just like string? 如何像字符串一样打印“$ 1”?

for example: 例如:

set -- "output"  # this sets $1 to be "output"
echo $1          # ==> output

But I want this: 但我想要这个:

echo $1      # ==> $1

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 '$1'

You need to either: 你需要:

  • Enclose the variable in SINGLE quotes: echo '$1' 将变量括在SINGLE引号中: echo '$1'
  • Escape the $ sign: echo "\\$1" 逃离$符号: echo "\\$1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM