0

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?

4

1 回答 1

2

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\"
"
于 2013-06-05T20:08:38.197 回答