1
[root@centos ~]# printf "#!/bin/sh\nsomething"
-bash: !/bin/sh\nsomething": event not found

It fails to execute the command as a command, because it does not get saved into the command history.

Must I obfuscate the hashbang slightly in order for it to let me get past?

I tried this:

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

(Also tried echo to the same effect)

4

2 回答 2

3

在交互式 shell 中,!用于历史替换。您可以使用\或通过将字符串放在单引号中来对其进行转义:

printf '#!/bin/sh\nsomething'

请参阅 bash History Expansion的文档。

如果您的实际应用程序将在脚本中,这将不是问题,因为默认情况下脚本中未启用历史记录。

如果您从不使用历史扩展,您可以使用以下命令禁用历史字符:

export histchars=
于 2013-07-26T03:36:37.097 回答
0

出色地。这是我确实找到的解决方法:

[root@centos ~]# printf "#%s/bin/sh\nsomething" !
#!/bin/sh
something

是的,一定是历史扩展搞砸了(提到 an 的错误event是一个很大的线索)

注意:我选择在我的脚本制作脚本中使用 heredoc。转义字符串是地狱般的。

于 2013-07-26T03:37:29.467 回答