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.

I'm currently on a Linux machine and the shell prompt is showing me the last return value and number of executed commands (picture included, with these numbers shown in purple).

My own computer doesn't have this, how can I configure it?
I'm using Xubunto, if more details are needed let me know -- I'm not much of a Linux user (I don't know what's relevant here).

example

share|improve this question
    
This is too vague to answer. What are these commands you're executing? What is your computer (OS) that doesn't have this? –  Chris Ting May 30 '11 at 17:16
    
I've said my OS is Xubuntu, and this is not related to any specific command, it's just something that always get showed. –  Eran May 30 '11 at 17:40

3 Answers 3

up vote 10 down vote accepted

It seems bash is Xubuntu's default shell.

Edit .bashrc or .bash_profile (depending on your system configuration) and look for a line starting with PS1=. This line sets your prompt.

To add the last command's return value, add the following to that line:

`echo -n $?`

so it looks e.g. like the following (my current prompt, simplified):

PS1='\u in \w (`echo -n $?`)\n -> \$ '

It will look like this, with _ being the cursor:

danielbeck in ~ (0)
 -> $ _

Alternatively, you can use the environment variable PROMPT_COMMAND to prepend the return code to your prompt:

export PROMPT_COMMAND='RET=$?; echo -n "($RET) "'

This will add e.g. (0) just before your otherwise not modified prompt.


You get the "counter" by adding \# to your prompt: it's the command number. More useful might be the history number, which doesn't start at 1, but allows you to execute any command by entering an exclamation mark, followed by the command's history number:

984 $ foo
-bash: foo: command not found
985 $ !984
foo
-bash: foo: command not found
986 $ _

After some additional playing:

PS1='`RET=$?; if [ $RET != 0 ] ; then echo "rc $?"; fi`\n\u in `pwd`\n#\# !\! \$ '

This will only show the return value if it's non-zero, on it's own line. The command number and history number are on the same line as the command you're going to enter:

danielbeck in /Users/danielbeck/Downloads
#1 !984 $ foo
-bash: foo: command not found
rc 127
danielbeck in /Users/danielbeck
#2 !985 $ _
share|improve this answer

The variable $? contains the exit code for the last run program.

ninth:~ sakkaku$ echo Hello World
Hello World
ninth:~ sakkaku$ echo $?
0
ninth:~ sakkaku$ cat asdasd
cat: asdasd: No such file or directory
ninth:~ sakkaku$ echo $?
1

I think you can get the "number of commands executed" by using an incrementer

ninth:~ sakkaku$ echo $[numcommands++]
0
ninth:~ sakkaku$ echo $[numcommands++]
1
ninth:~ sakkaku$ echo $[numcommands++]
2
ninth:~ sakkaku$ echo $[numcommands++]
3

Then you will need to modify the PS1/PS2 variable in your .bashrc to change the prompt. This seems like a decent guide (except it recommends modifying /etc/bashrc, I would just do ~/.bashrc).

share|improve this answer
1  
Thanks, I'll try it later when I'm at my own computer and will see how it goes. –  Eran May 30 '11 at 17:40

FYI, if you use zsh it's even easier. This construct: %(?..%F{red}%?%f) means show the last commands errorlevel in red if it's not zero (normal), but if it's normal show nothing. The construct %(1j.[%j].) means show the current number of background jobs in square brackets.

share|improve this answer

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.