Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress parameter expansion in a here-doc in Bash?

Tags:

bash

I'd like to do the following in bash, but without having variables interpolated:

cat >aBashScript.sh <<EOL
$name
EOL

The file should contain $name, but instead it's empty. How does one do that?

like image 648
Josh Nankin Avatar asked Jul 24 '13 16:07

Josh Nankin


2 Answers

You can disable parameter expansion in here documents by quoting the limit string:

cat >aBashScript.sh <<'EOL'
$name
EOL
like image 194
Michael Avatar answered Oct 25 '22 21:10

Michael


You need to escape the dollar sign, simply prefix it with a backslash to escape it like so:

cat >aBashScript.sh <<EOL
\$name
EOL

Or disable quoting as @Michael suggested.

like image 32
Geoffrey Avatar answered Oct 25 '22 22:10

Geoffrey