Bash 在打印特殊字符时卡住
我的 .profile 中有这个函数,它使用 iTerm2 创建一个咆哮通知:
function growl()
{
echo -n -e $'\e]9;'"$@"'\007'
}
但是,如果我声明 -f groll ,Bash 将陷入困境,您唯一的选择就是关闭当前终端窗口。
iTerm2 中的输出 + 卡住:
growl ()
{
echo -n -e '
终端 + 响铃中的输出:
growl ()
{
echo -n -e '
7;file://path/to/current/work/dir
I have this function in my .profile, which creates a growl notification using iTerm2:
function growl()
{
echo -n -e
But, if I do declare -f growl
, Bash will become stuck and your only option is to close the current terminal window.
Output in iTerm2 + getting stucked:
growl ()
{
echo -n -e '
Output in Terminal + bell:
growl ()
{
echo -n -e '
7;file://path/to/current/work/dir
\e]9;'"$@"'\007'
}
But, if I do declare -f growl
, Bash will become stuck and your only option is to close the current terminal window.
Output in iTerm2 + getting stucked:
Output in Terminal + bell:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您可能知道的那样,declare -f groll 正在打印函数的完整定义,包括控制字符。
我建议更改定义,以便它打印您想要的字符,而不必将它们逐字包含在函数定义中。 Bash 的
echo
命令采用-e
选项,使其能够识别转义序列,例如贝尔字符的\a
。info bash
并搜索“echo”以获取更多信息。(如果您问一旦弄乱终端模拟器如何恢复,我没有答案。)
编辑:我误读了这个问题。问题是您使用的是
$'...'
语法,它将转义序列扩展为实际的控制字符。显然declare -f
正在打印扩展的字符串。只是不要使用$'...'
语法。(
'\a'
相当于'\007'
。)EDIT2:修复了拼写错误
EDIT3:显然在某些版本的 bash 中,
echo -n -e ' \e'
不会打印转义字符,但 '\033' 可以工作。修改后的解决方案:(提问者使用的是 bash 3.2.48。我已经确认 3.2.25 可以正确执行此操作。这很奇怪。)
declare -f growl
, as you probably know, is printing the complete definition of the function, including the control characters.I suggest changing the definition so it prints the characters you want without having to have them literally included in the function definition. Bash's
echo
command takes a-e
option that enables it to recognize escape sequences such as\a
for the bell character.info bash
and search for "echo" for more information.(If you're asking how to recover once you've messed up your terminal emulator, I don't have an answer.)
EDIT: I misread the question. The problem is that you're using the
$'...'
syntax, which expands escape sequences to actual control characters. Apparentlydeclare -f
is printing the expanded strings. Just don't use the$'...'
syntax.(
'\a'
is equivalent to'\007'
.)EDIT2: Fixed typo
EDIT3: Apparently in some versions of bash,
echo -n -e '\e'
doesn't print an escape character, but '\033' does work. Revised solution:(The questioner is using bash 3.2.48. I've confirmed that 3.2.25 does this correctly. It's odd.)
最终解决方案:
将其括在单引号内并使用 eval:
Final solution:
Wrap it inside single-quotes and use eval: