rvk
rvk

Reputation: 767

Is it possible to fetch the last command ran in a parent terminal?

I am trying to programmatically fetch history from a bash terminal from within a child process (go executable). I'm essentially looking for the last N commands the terminal has run.

Flow:

  1. open bash terminal PID=5
  2. run a few commands (sed, rpm, gpg)
  3. run go executable PID=32 PPID=5
  4. fetch commands and parameters (sed, rpm, gpg) from parent process (bash terminal) and print them

The bash built-in history does not yield proper results when run under go/shell out.

Reading ~/.bash_history is not accurate as the history from a bash terminal is written when the prompt is closed.

Wondering if this is possible at all?

Upvotes: 0

Views: 293

Answers (1)

EarthDragon
EarthDragon

Reputation: 755

As suggested here you can save the new commands to a file with history -a <file_path>, then in the child process you can read the last N lines from the file (with tail -n <N>).

Upvotes: 1

Related Questions