1

For training, I have idea to write a script which will display the last bash/zsh command.

First of all, I tried with os.system and subprocess to execute history command. But, as you know, history is a shell builtin, so, it doesn't return anything.

Then, I tried this piece of code:

shell_command = 'bash -i -c "history -r; history"' event = Popen(shell_command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)

But it have just shown commands from last session. What i want to see is the previous command (which i just typed) I tried cat ~/.bash_history and have same result, unluckily.

Any idea ?

10
  • 1
    What did you expect/want it to show? Commented Aug 21, 2015 at 17:45
  • What happens if you put these commands in a shell script and run them? Do you get the output that you want? Commented Aug 21, 2015 at 18:05
  • @EricRenouf I's sorry if I made you confuse. but, i want it show the previous command, not commands in previous bash session Commented Aug 21, 2015 at 18:07
  • @dimo414 I tried it before, but it doesnt work, too :( Commented Aug 21, 2015 at 18:10
  • So your problem is that the contents of the history are not what you're expecting? Bash does not write to .bash_history after every command, so you may just be getting the right results, but not quite what you want. You could try running history -a before launching python to write that session to the file if that's what you want to see Commented Aug 21, 2015 at 18:14

1 Answer 1

2

You could use tail to get the last line:

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)
out = Popen(["tail", "-n", "1"], stdin=event.stdout, stdout=PIPE)

output = out.communicate()
print(output[0])

Or just split the output and get the last line:

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)
print(event.communicate()[0].splitlines()[-1])

Or read bash_history:

from os import path
out= check_output(["tail","-n","1",path.expanduser("~/.bash_history")])
print(out)

Or open the file in python and just iterate until you get to the end of the file:

from os import path
with open(path.expanduser("~/.bash_history")) as f:
    for line in f:
        pass
    last = line
    print(last)
Sign up to request clarification or add additional context in comments.

3 Comments

I'm very sorry if i made you confuse, but, i just dont know how to get previous command, not commands from previous bash session. anw, thanks for your answer
Yes. For example, after running cat smtfile command, i run my script and what it returns to me should contain cat smtfile
Ok, then that is a completely different story, if it were for your own use you can add export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r" to your .bashrc file as described here unix.stackexchange.com/a/48113

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.