Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print $ in shell script?

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
like image 236
LOKESH Avatar asked Jul 30 '14 12:07

LOKESH


People also ask

Can we use print in shell script?

The bash printf command is a tool used for creating formatted output. It is a shell built-in, similar to the printf() function in C/C++, Java, PHP, and other programming languages. The command allows you to print formatted text and variables in standard output.

How do you print numbers in shell script?

Use printf to format the number of digits you want to print.

How do I print text in bash?

Print String in Bash To print a string in Bash, use echo command. Provide the string as command line argument to echo command.


3 Answers

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

like image 80
anubhava Avatar answered Nov 09 '22 14:11

anubhava


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)

like image 30
Basile Starynkevitch Avatar answered Nov 09 '22 15:11

Basile Starynkevitch


Pretty simple:

Input:

echo '$msg1' two three    

(note the single quotes)

Output:

$msg1 two three
like image 43
Sidharth Menon Avatar answered Nov 09 '22 16:11

Sidharth Menon