我的最终目标是捕获在终端中执行的上一个命令。由于~/.bash_history文件不包括当前终端会话中的命令,因此我不能简单地读取该文件。
在另一个线程中,我找到了这个脚本:
from subprocess import Popen, PIPE, STDOUT
shell_command = 'bash -i -c "history -r; history"'
event = Popen(shell_command, shell=True, stdin=PIPE, stdout=PIPE,
stderr=STDOUT)
output = event.communicate()
这与我所寻找的非常接近,但它也不会包括当前终端会话的历史记录,因为它是作为子进程启动的。有没有办法在当前 shell 中执行类似的命令?
history
不是一个可执行文件,而是一个 shell 内置命令。 - Wooble