-3

I'm trying to get only the last command ran in a different Bash window. I have the other Bash window's PID. I can run

APID=<bash pid number>; gdb -batch --eval "attach $APID" --eval "call write_history(\"/tmp/bash_history-$APID.txt\")" --eval 'detach' --eval 'q'

but it writes that to a file.

What would be an sh command that would output the last line of the result of that function call without creating a temporary file?

6
  • 1
    man tail is your friend.
    – user9517
    Sep 19, 2014 at 19:47
  • @funkodebat tail -1 ~/.bash_history
    – alexus
    Sep 19, 2014 at 19:49
  • 1
    @alexus: .bash_history is usually only written when the shell exits so a simple tail probably won't work. Sep 22, 2014 at 16:04
  • @DennisWilliamson You most likely right, but that's what OP asked for.
    – alexus
    Sep 22, 2014 at 16:20
  • @alexus: I dont think so. It looks like he's asking gdb to tell Bash to write the history, which is contained in memory, of a currently running shell. In fact, I just tested it, and that's what it does. And tail -n 1 ~/.bash_history doesn't show the most recent command from the test session. Sep 22, 2014 at 16:32

1 Answer 1

1

Try the following (it would not create a temporary file and will print the last line):

APID=<bash pid number> gdb -batch --eval "attach $APID" --eval "call write_history(\"/dev/stdout\")" --eval 'detach' --eval 'q' | tail -1
1
  • /dev/stdout works for me, but note that it outputs to the terminal being inspected. If you want it to output to another terminal, perhaps the one running the gdb command, use the specific terminal device instead of /dev/stdout (e.g. /dev/pts/15). Sep 22, 2014 at 16:53

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