Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

What formatting character needs to be added to a bash prompt to indicate the most recent history item run (or the current command number if 1 could be added to the last history entry)?

My prompt string is this:

\[\033[33m\][\u@\[\033[1;31m\]\h]\]\033[0m {\W}\n\033[1;34m\]\w\]\033[0m >

Gives me the following display:

[user@host] {~}
~ >

User is in yellow, and the host is in red. The entry in brackets is the current directory, and the entry before the greater-than sign is the full pwd.

Can I append to the first line the current command number so I would have something like the following:

[user@host] {~} (nnn)
~ >

where (nnn) is the current (or just processed) command number, as shown when running history?

share|improve this question
up vote 6 down vote accepted

From the PROMPTING section of the bash man page:

\!     the history number of this command
\#     the command number of this command

and further down:

The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file ..., while the command number is the position in the sequence of commands executed during the current shell session.

If you want the last command number, you could wrap the number in an arithmetic evaluation like this:

export PS1='last cmd num: $((\# -1)), this num: \# '
share|improve this answer
thanks, Doug. The man page is nutty long :) – warren May 7 '10 at 18:50

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.