Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

up vote 3 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

 
discard

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.