${1}' Note that using
'...">

Bash: single quotes and double quotes and exclamation mark

I have a simple script called example :

 #!/bin/sh echo $'${1}' 

Note that using $'' here means converting \n to a new line. ${1} is the first parameter passed to this shell script.

I want to pass a parameter to this script example and print the following:

 #1. You're smart! #2. It a difficult question! 

I tried the following:

 example "#1. You're smart!\n#2. It a difficult question!" 

Error: -bash: !\n#2.: event not found

Then I tried to escape ! one quote and tried:

 example '#1. You're smart\!\n#2. It a difficult question\!' 

It outputs:

 ${1} 

Any solution here? Thank you very much!

+2
source share
2 answers
 $ cat t.sh #! /bin/bash echo -e $@ 

echo -e $1 or echo -e ${1} if you just want to handle the first argument.

To stop bash trying to deploy ! use set +H (see In bash, how do I avoid an exclamation mark? )

 $ set +H $ ./t.sh "#1. You're smart!\n#2. It a difficult question!" #1. You're smart! #2. It a difficult question! 
+3
source

What's inside the $'' expression must be a literal. You cannot extend other variables in it.

But you can do this:

 echo "${1//\\n/$'\n'}" 

Jan Hoodek has an even better answer:

 echo -e "$1" 
+2
source

Source: https://habr.com/ru/post/1385446/


All Articles