I'm interested in getting the just-run command in PROMPT_COMMAND, is there any better way of doing so beyond history 1?

More generally, what information is available at the time PROMPT_COMMAND is executed? I know that $? is the exit code of the just-run command, but I'm not sure what else is available.

  • everything what available in bash Aug 25, 2016 at 21:36
  • 1
    !! or !-1 - refer to the Event Designators subsection under the HISTORY EXPANSION section of the bash manual Aug 25, 2016 at 23:12
  • @IporSircer that isn't the question. I realize PROMPT_COMMAND is a Bash expression. The question is about the context the expression is evaluated in.
    – dimo414
    Aug 26, 2016 at 3:29
  • You can find out what's available with PROMPT_COMMAND="set >/tmp/prompt_command.log". Aug 26, 2016 at 14:58
  • @MateiDavid good idea thanks. I posted an answer with the result of running set from PROMPT_COMMAND, and it indicates there are no environment variables being set.
    – dimo414
    Aug 26, 2016 at 15:41

1 Answer 1


As mentioned $! will contain the exit code of the last command when PROMPT_COMMAND is executed. Matei David had a good suggestion to run set from the PROMPT_COMMAND, which reveals there's nothing (on my system) being added to the environment:

$ PROMPT_COMMAND="set > /tmp/prompt.log"

$ set > /tmp/basic.log

$ diff -u0 /tmp/basic.log /tmp/prompt.log 
# no results

Of course this is anecdotal and doesn't include any Bash magic variables or other state not included by set, but it's a start.

You can get the last command with:

last_command=$(HISTTIMEFORMAT='' history 1 | sed 's/ *[0-9]* *//')

You must log in to answer this question.

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