0

writing a python script to generate commands is working and the commands are getting executed in bash, but they are not getting entered in .bash_history.What should I do so that those commands are shown in .bash_history ? my py file is as follows:-

import os
import sh

def main():
    bashcommand = "top"
    bashcommand1 = "ls"
    bashcommand2 = "vi exp1.txt"
    os.system(bashcommand)
    os.system(bashcommand1)
    os.system(bashcommand2)
main()

sh.cd('/root')
print(sh.pwd())
2
  • Please take a look at editing-help.
    – Cyrus
    Mar 5, 2016 at 4:46
  • os.system executes commands using /bin/sh. When Bourne Again Shell is called as /bin/sh, it does not write anything to history. See os.system and system(3).
    – user260419
    Mar 5, 2016 at 13:59

1 Answer 1

0

As Sami Laine commented, os.system uses a shell that is often not bash. on top of this, bash -c 'command' does not write to .bash_history. You could, of course, just make something like this:

import os
def runcommand(command):
    with open(os.path.expanduser('~/.bash_history'), 'a') as f:
        f.writelines([command])
    os.system(command)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .