如何使用python向当前终端历史添加命令?

如何使用python向当前终端历史添加命令?,python,linux,bash,python-3.x,terminal,Python,Linux,Bash,Python 3.x,Terminal,我已经阅读了其他答案,命令是history-s command。 我还看到了一个示例,下面的示例用于通过python访问history shell_command = 'bash -i -c "history -r; history"' event = Popen(shell_command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT) output = event.communicate() print(output)

我已经阅读了其他答案,命令是
history-s command
。 我还看到了一个示例,下面的示例用于通过python访问
history

shell_command = 'bash -i -c "history -r; history"'      
event = Popen(shell_command, shell=True, stdin=PIPE, stdout=PIPE, 
stderr=STDOUT)
output = event.communicate()
print(output)
但是我不能以同样的方式使用
history-s命令

我的主要目标是将某个命令附加到用户的当前历史记录中,这样当用户按下
向上
按钮时,
“命令”
就会显示出来。

这将起作用

import subprocess
command = "grep"
shell_command = 'bash -i -c "history -s; %s"' % command
event = subprocess.Popen(shell_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = event.communicate()
print(output)

grep
有效。但是它不会出现在终端
历史记录中;你不能从外部影响它。@chepner那么这就不可能了?