Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The $? variable holds the exit status of last run command. Is there a variable that holds last run command itself?

share|improve this question
Related question on Stack Overflow: BASH: echoing the last command run – Gilles Oct 3 '11 at 21:11
up vote 2 down vote accepted

Use fc to get the previous command line. It is normally used to edit the previous command line in your favourite editor, but it has a "list" mode, too:

last_command="$(fc -nl -1)"

share|improve this answer

If the last command was executed without arguments, it'll be saved in the $_ variable. This normally contains the last argument of previous command - so if there were no arguments, the value of $_ is the last command itself.

Another option is to learn the details of last background command. As l0b0 wrote, $! holds its PID - so you can parse the output of ps $! (possibly with additional formating options to ps).

share|improve this answer

No, but you can get it during execution to store for other commands:

  • $0: Path of the current shell script.
  • $FUNCNAME: "Name of the current function."
  • "$@": All the parameters of the current command, quoted separately.
  • $!: "PID (process ID) of last job run in background."
  • $$: "Process ID (PID) of the script itself."

The full command of the current script should therefore be "$0" "$@". If it's a function it should be "$FUNCNAME" "$@". You might want to store that in an array for future processing. For example, store this in test.sh:

#!/usr/bin/env bash
    declare -a command=("$0")
    for param in "$@"
        command+=("$(printf %q "$param")")
    echo "${command[@]}"
foo "$@"

When running ./test.sh "first argument" "second argument", it should return:

./test.sh first\ argument second\ argument

Which are equivalent calls.

share|improve this answer
In bash there is a BASH_COMMAND variable, but do not seems to be useful in any way, apart from use in traps. – enzotib Oct 3 '11 at 8:26
Thanks for your input. What about if I run some-command in a shell script, and it fails. I'll have non-zero status in $?, will "no" still hold for the existence of variable holding some-command? – Eimantas Oct 3 '11 at 9:10
As far as I know, the sole fact that a command failed does not change the set of information your shell stores about it. So I'd say Yes, "no". – rozcietrzewiacz Oct 3 '11 at 11:36

The DEBUG trap lets you execute a command right before any simple command execution. A string version of the command to execute (with words separated by spaces) is available in the BASH_COMMAND variable.

trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
echo "last command is $previous_command"

Note that previous_command will change every time you run a command, so save it to a variable in order to use it. If you want to know the previous command's return status as well, save both in a single command.

cmd=$previous_command ret=$?
if [ $ret -ne 0 ]; then echo "$cmd failed with error code $ret"; fi

If you only want to abort on a failed commands, use set -e to make your script exit on the first failed command. You can display the last command from the EXIT trap.

set -e
trap 'echo "exit $? due to $previous_command"' EXIT

An alternate approach that might work for some uses is to use set -x to print a trace of the script's execution and examine the last few lines of the trace.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.