Question

When creating heredocs in bash using the $( construct then newlines are being changed to dollar signs $.

stephenb@gondolin:/shared/git2/etl/appminer$ IRB=$(cat -e<<'HERE'
> require 'irb/ext/save-history'
> IRB.conf[:SAVE_HISTORY] = 100
> IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
> HERE
> )
stephenb@gondolin:/shared/git2/etl/appminer$ echo $IRB
require 'irb/ext/save-history'$ IRB.conf[:SAVE_HISTORY] = 100$ IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"$

Trying to print the variable inside of double quotes does convert to newlines but still retains the dollar sign:

stephenb@gondolin:/shared/git2/etl/appminer$ echo "$IRB"
require 'irb/ext/save-history'$
IRB.conf[:SAVE_HISTORY] = 100$
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"$

Note: this is a followup to an SOF question about heredoc variables in bash: followingHow to assign a heredoc value to a variable in Bash?

Was it helpful?

Solution

If you want to remove the dollar signs, don't use cat with -e switch. It's -e that marks end-of-line with $. It also does other things.

Why do you want to assign a here-doc into a variable? Doesn't a normal assignment do the job more easily? For example:

stephenb@gondolin:/shared/git2/etl/appminer$ IRB="require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = \"#{ENV['HOME']}/.irb-save-history\"
"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top