How to print $ in shell script?

回眸只為那壹抹淺笑 提交于 2020-01-15 07:19:07

问题


I want output as $msg1 two three. No space between $ and msg1. How it possible?

#!/bin/sh
msg1=$
ms="$msg1 msg1"
msg2="$ms two"
msg3="$msg2 three"
echo $msg3

回答1:


You can use:

msg1='$'
ms="${msg1}msg1"
msg2="$ms two"
msg3="$msg2 three"
echo "$msg3"

OUTPUT:

$msg1 two three

PS: Take note of ${msg1} syntax to create variable boundary around msg1. This is used to avoid it making it $msg1msg1




回答2:


Just quote the $ (or also the word around it). E.g.

 echo '$'

 echo 'some$inside'

If you want a message without newline, use echo -n

See echo(1) and bash(1)




回答3:


Pretty simple:

Input:

echo '$msg1' two three    

(note the single quotes)

Output:

$msg1 two three


来源:https://stackoverflow.com/questions/25036555/how-to-print-in-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!