I have a simple script named example
:
#!/bin/sh
echo $'${1}'
Please note that the usage of $''
here is to convert \n
into new line.
${1}
is the first parameter passed to this shell script.
I want to pass a parameter to this script example
and it prints the following:
#1. You're smart!
#2. It's a difficult question!
I tried the following:
example "#1. You're smart!\n#2. It's a difficult question!"
An error: -bash: !\n#2.: event not found
Then I tried to escape !
by single quote, and tried:
example '#1. You're smart\!\n#2. It's a difficult question\!'
It outputs:
${1}
Any solution here? Thanks a lot!
What's inside a
$''
expression has to be a literal. You can't expand other variables inside it.But you can do this:
Jan Hudec has an even better answer:
Or
echo -e $1
, orecho -e ${1}
if you just want to process the first argument.To get bash to stop trying to expand
!
, useset +H
(see In bash, how do I escape an exclamation mark?)