python - 在 Python 中运行 BASH 内置命令?

标签 python bash command subprocess

有没有办法从 Python 运行 BASH 内置命令?

我试过了:

subprocess.Popen(['bash','history'],shell=True, stdout=PIPE)

subprocess.Popen('history', shell=True, executable = "/bin/bash", stdout=subprocess.PIPE)

os.system('history')

及其许多变体。我想运行 historyfc -ln

最佳答案

我终于找到了一个可行的解决方案。

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()

感谢大家的意见。

关于python - 在 Python 中运行 BASH 内置命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5460923/

相关文章:

Python 日期时间添加

linux - 删除 bash 脚本中的 tar.gz 文件

mysql - 使用 bash 选择 mysql 查询

shell - 如何查找同名子文件夹的磁盘大小

command - 如何在atom启动时运行命令

python - 仅从数据框中选择每个月的最后一周 - Python/Pandas

python - Google App Engine 连接两个表的结果

python - 对索引的二维数组的值求和

Bash 脚本不工作

Java运行时执行