How to print $ in shell script?

2019-07-11 04:31发布

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

3条回答
可以哭但决不认输i
2楼-- · 2019-07-11 05:05

Pretty simple:

Input:

echo '$msg1' two three    

(note the single quotes)

Output:

$msg1 two three
查看更多
手持菜刀,她持情操
3楼-- · 2019-07-11 05:24

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

查看更多
放我归山
4楼-- · 2019-07-11 05:26

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)

查看更多
登录 后发表回答