Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print literal string "$1" in bash script?

Tags:

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?

for example:

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

But I want this:

echo $1      # ==> $1 
like image 551
Ziyaddin Sadigov Avatar asked May 08 '13 16:05

Ziyaddin Sadigov


People also ask

How do I print a string in Bash?

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

What does [- Z $1 mean in Bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script.

What is $2 in Bash?

$2 is the second command-line argument passed to the shell script or function. Also, know as Positional parameters.

What does $_ mean in Bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.


2 Answers

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:

$ echo '$1' 
like image 162
fedorqui 'SO stop harming' Avatar answered Sep 20 '22 17:09

fedorqui 'SO stop harming'


You need to either:

  • Enclose the variable in SINGLE quotes: echo '$1'
  • Escape the $ sign: echo "\$1"
like image 26
Carlos Campderrós Avatar answered Sep 21 '22 17:09

Carlos Campderrós