2013-07-26 72 views
1
[[email protected] ~]# printf "#!/bin/sh\nsomething" 
-bash: !/bin/sh\nsomething": event not found 

無法將命令作爲命令執行,因爲它不會保存到命令歷史記錄中。從shell或shellcript寫入shell腳本?

我必須稍微混淆hashbang才能讓我過去嗎?

我嘗試這樣做:

[[email protected] ~]# printf "%s!/bin/sh\nsomething" # 
-bash: !/bin/sh\nsomething": event not found 

(也試過echo達到同樣的效果)

回答

3

在交互shell,!用於歷史替換。您可以\或通過將字符串中的單引號轉義:

printf '#!/bin/sh\nsomething' 

見的bash History Expansion的文檔。

如果您的實際應用程序將位於腳本中,這不會成爲問題,因爲腳本中默認情況下未啓用歷史記錄。

export histchars= 
+0

哦廢話。是否真的是導致問題的歷史替代?那麼,換行似乎用單引號,所以看起來不錯。 –

0

好:

如果不用不完歷史擴展,你可以禁用歷史人物。這是一種變通方法,我發現:

[[email protected] ~]# printf "#%s/bin/sh\nsomething" ! 
#!/bin/sh 
something 

是的,必須是歷史擴展是與它(錯誤提的event是一大線索)搞亂

注:我選擇使用heredoc在我的腳本製作腳本中。轉義字符串是地獄般的。