本文介绍了回声“#!"失败——“未找到事件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下失败,我不明白为什么:

The following fails and I don't understand why:

$ echo "#!"

以下也失败并显示相同的错误消息:

the following also fails with the same error message:

$ echo "#!"

错误信息:

-bash: !": event not found

为什么会失败?echo 应该怎么做?

Why does it fail? How should the echo be done instead?

推荐答案

! 字符用于 csh 风格的历史扩展.

The ! character is used for csh-style history expansion.

如果您不使用此功能,set +o histexpand(又名 set +H)会关闭此行为.它对脚本关闭,但通常为交互使用启用.在这种情况下,我个人的建议是通过将 set +o histexpand 添加到您的 .bash_profile(或 .bashrc,如果您没有 .bash_profile;这比我想放入括号中要复杂得多).

If you do not use this feature, set +o histexpand (aka set +H) turns off this behavior. It is turned off for scripts, but often enabled for interactive use. In such cases, my personal recommendation is to turn it off permanently by adding set +o histexpand to your .bash_profile (or .bashrc if you don't have a .bash_profile; this is more complex than I want to try to fit into a parenthetical).

作为一种解决方法,如果由于某种原因您不能或不想关闭并忘记此旧的 csh 功能,您可以使用单引号代替双引号 -- 请记住当然,它们的语义不同.例如,如果需要将引用与变量插值结合使用,则可以更改

As a workaround, if for some reason you cannot or don't want to turn off and forget about this legacy csh feature, you can use single quotes instead of double quotes -- keeping in mind, of course, their different semantics. If you need to combine quoting with variable interpolation, for example, you can change

echo "#!$SHELL"  # oops, history expansion breaks this

进入

 echo '#!'"$SHELL"

(注意相邻的单引号和双引号字符串;shell 完成此操作后,引号将被删除,并且字符串 #! 将在值的旁边输出变量 SHELL 之间没有空格)或许多其他常见的解决方法,例如

(notice the adjacent single-quoted and double-quoted strings; after the shell is done with this, the quotes will be removed and the string #! will be output next to the value of the variable SHELL with no space between them) or a number of other common workarounds like

 printf '#!%s
' "$SHELL"

这篇关于回声“#!"失败——“未找到事件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:04