使用$(构造然后将换行符更改为美元符号$。)在bash中创建heredoc时。
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"$
尝试在双引号内打印变量确实会转换为换行但仍保留美元符号:
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"$
注意:这是关于bash中关于heredoc变量的SOF问题的后续跟随:How to assign a heredoc value to a variable in Bash?
答案 0 :(得分:2)
如果您要删除美元符号,请不要将cat
与-e
切换一起使用。它是-e
,用$表示行尾。它还做其他事情。
为什么要将here-doc分配给变量?正常的任务不是更容易完成工作吗?例如:
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\"
"