这似乎与以下事实有关:bash -c
开始一个非交互式壳。这可能会阻止它恢复最终状态。
要显式启动交互式 shell,您只需传递-i
bash 选项。
$ cat test_read.py
#!/usr/bin/python3
from subprocess import Popen
p = Popen(['bash', '-c', 'read -s foo; echo $foo'])
p.wait()
$ diff test_read.py test_read_i.py
3c3
< p = Popen(['bash', '-c', 'read -s foo; echo $foo'])
---
> p = Popen(['bash', '-ic', 'read -s foo; echo $foo'])
When I run and press Ctrl+C:
$ ./test_read.py
我得到:
Traceback (most recent call last):
File "./test_read.py", line 4, in <module>
p.wait()
File "/usr/lib/python3.5/subprocess.py", line 1648, in wait
(pid, sts) = self._try_wait(0)
File "/usr/lib/python3.5/subprocess.py", line 1598, in _try_wait
(pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt
并且终端未正确恢复。
如果我运行test_read_i.py
文件以与我刚刚得到的相同的方式:
$ ./test_read_i.py
$ echo hi
hi
没有错误,终端可以工作。