如何使用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那么这就不可能了?