在 Bash 或者 Zsh 中,当使用 stty raw -echo 命令时,可能会遇到一些问题。这个命令通常用于更改终端设备的行为,以启用类似 vim 或类似于 Expect 的应用程序中的特殊键处理。但是,当你运行交互命令时,可能会遇到一些意外情况。
以下是一个 Bash 脚本中的示例代码,其中使用了 stty raw -echo 命令:
#!/bin/bash
# Turn off input line buffering and echoing
stty raw -echo
# Enable the special characters for arrow keys
echo -e "\x1b[?1h\x1b="
read input
# Turn input line buffering and echoing back on
stty -raw echo
echo "You typed: $input"
运行这个脚本,你会发现当你输入回车键时,屏幕上不会有换行符。这是因为 stty raw 选项禁用了行缓冲和回显,所以每次按键都直接写入终端。
为了解决这个问题,你可以使用以下代码将 stty raw -echo 命令更改为 stty -icanon echo,这个命令会禁用行缓冲,但仍会启用回显。这将确保回车键的正常行为。
#!/bin/bash
# Turn off input line buffering and echoing
stty -icanon echo
# Enable the special characters for arrow keys
echo -e "\x1b[?1h\x1b="
read input
# Turn input line buffering and echoing back on
stty icanon -echo echo
echo "You typed: $input"
现在你可以输入回车键并按预期的方式读取输入了。
上一篇:Bash/正则表达式:当一些第一个字段以引号开头和有逗号时,替换CSV文件中的第二个字段。
下一篇:bash3rdparty/osx/install_deps.sh?的含义是需要在OSX系统上安装依赖项,但是指令具体内容不可确定,需要查看该文件中的代码示例来进一步理解