2013-06-05 77 views
0

節省定界符變量時使用$(建造,然後在bash創建here文檔的換行符被改爲美元符號$。特殊性在慶典

[email protected]:/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 
>) 
[email protected]:/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"$ 

要打印的變量中的雙引號不轉換到換行符但仍保留了美元符號:

[email protected]:/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定界符變量的SOF問題:以下How to assign a heredoc value to a variable in Bash?

回答

3

如果要刪除美元符號,請勿使用cat-e開關。這是-e,標記爲$尾。它也做其他事情。

爲什麼你想分配一個here-doc到一個變量中?不正常的任務是否能更輕鬆地完成這項工作?例如:

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