MadhaviJ
MadhaviJ

Reputation: 2345

Invoking history command on terminal via Python script

I am running a python script which is using subprocess to execute "history" command on my Ubuntu terminal. Apparently,I am getting this error

history: not found

I got to know that history can not be invoked by any script by default. What can I do to overcome this? Or any other possible alternatives.

readline.get_history_item() method isnt working either.

Upvotes: 1

Views: 1378

Answers (1)

Kenly
Kenly

Reputation: 26688

Use this:

from subprocess import Popen, PIPE, STDOUT


e = Popen("bash -i -c  'history -r;history' ", shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = e.communicate()

Upvotes: 4

Related Questions