Linux 终端: 输入反馈消失,断行不显示

有时我不得不运行一个命令行工具(Python 脚本) ,它的输出似乎会破坏我的终端。 执行结束后,输入反馈消失了(我看不到我在输入什么) ,而且不显示换行符。如果通过 Putty远程启动终端,以及使用 gnome-terminal在本地启动终端,都会发生这种情况。

例如,在问题发生后,如果我键入 ENTER pwd ENTER,我希望看到:

[userA@host006 ~]$
[userA@host006 ~]$ pwd
/home/userA
[userA@host006 ~]$

但实际上结果是:

[userA@host006 ~]$ [userA@host006 ~]$ /home/userA
[userA@host006 ~]$

唯一的解决办法就是关闭那个终端,重新启动一个。

也许是相关的: 脚本输出包含一些基于终端的格式(例如,反转前景/背景以突出显示一些状态消息)。如果我将这个输出转储到一个文件中,我可以看到类似 [07mSome Message Here[0m的内容。

有什么办法能阻止这一切吗?

41846 次浏览

Execute the command reset and your terminal should be restored (reference).

This issue happens generally when dumping binary data to the terminal STDOUT which when the escape codes received are processed can do anything from change the color of the text, disable echo, even change character set.

The easy way to avoid this is to ensure you do not dump unknown binary data to the terminal, and if you must then convert it to hexadecimal to ensure it doesn't change the terminal settings.

To elaborate on Joshua Briefman's answer, executing reset -c will only reset the control characters responsible for your problem:

tset, reset - terminal initialization

Usage: tset [options] [terminal]


Options:
-c          set control characters
-e ch       erase character
-I          no initialization strings
-i ch       interrupt character
-k ch       kill character
-m mapping  map identifier to type
-Q          do not output control key settings
-r          display term on stderr
-s          output TERM set command
-V          print curses-version
-w          set window-size

Also note the following form the command's manual:

Note, you may have to type

<LF>reset<LF>

(the line-feed character is normally control-J) to get the terminal to work, as carriage-return may no longer work in the abnormal state. Also, the terminal will often not echo the command.