On Ubuntu, I would like to end up with a disk file that reads:
foo $(bar)
I would like to create this file using the cat
command, e.g.
cat <<EOF > baz.txt type_magic_incantation_here_that_will_produce_foo_$(bar) EOF
The problem is the dollar sign. I have tried multiple combinations of backslash, single-quote, and double-quote characters, and cannot get it to work.
Dollar escaping: Provide means to escape $ in multiline strings. The nicest and the most natural way to do it is via the same \$ sequence as in single line strings (no need to learn new syntax), but it is a backwards-incompatible change that will take years to get gradually introduced.
We can see that whatever texts we've entered into the standard input stream will be echoed to the output stream by the cat command. Once we are done, we can terminate the command by pressing CTRL+D.
The answer is to use either \$ or single quotes.
You can use regular quoting operators in a here document:
$ cat <<HERE > foo \$(bar) > HERE foo $(bar)
or you can disable expansion in the entire here document by quoting or escaping the here-doc delimiter:
$ cat <<'HERE' # note single quotes > foo $(bar) > HERE foo $(bar)
It doesn't matter whether you use single or double quotes or a backslash escape (<<\HERE
); they all have the same effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With