Super User is a question and answer site for computer enthusiasts and power users. 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

I read this great article on customizing the bash prompt:

http://blog.superuser.com/2011/09/21/customizing-your-bash-command-prompt/

I also read about using 256 colors in bash:

http://www.logilab.org/blogentry/20255

Now, here's what my .bashrc looks like:

# Extended 256 colors
ext-color() {
    echo -ne "\[\033[38;5;$1m\]$2\[\033[m\]"
}

# Set a fancy prompt
PS1="`ext-color 172 \u` in `ext-color 172 $(pwd)`"

This get's me really close to having an orange prompt, but the username isn't being printed. Instead it looks like this:

u in /home/dave

How do I modify this to print the username of the currently logged-in user? I mean like this:

dave in /home/dave
share|improve this question
up vote 2 down vote accepted

It's much simpler than you're making it out to be. When you run the command ext-color 172 \u (either as part of setting PS1 or on its own), the shell parses \u, removing what it thinks is an irrelevant \, before passing it to ext-color as $2. The solution is simple: enclose it in quotes before passing it to ext-color:

PS1="$(ext-color 172 '\u') in $(ext-color 172 '\w')"

(Note that I've also replaced the backquotes with $( ), used \w instead of $(PWD), and quoted '\w' for the same reason as '\u'.)

share|improve this answer
    
Changed the accepted answer since this addresses what I was actually asking. Thanks. – Dave Kennedy May 5 '13 at 5:20

Adapting the approach that seems to work in the second part of the prompt, try e.g.:

PS1="`ext-color 172 $(whoami)` in `ext-color 172 $(pwd)`"

or even simpler:

PS1="`ext-color 172 $USER` in `ext-color 172 $(pwd)`"

or even nicer:

PS1="$(ext-color 172 "$USER") in $(ext-color 172 "$(pwd)")"

The last version won't cut off the part after a potential space in current directory because of the quotes around the pwd call.


EDIT: That won't update the working directory, as noted in the comment. I didn't even think of that since the question specifically asked about the user part (it's late here :-) ).

The easiest ("correct", "only") way is to just write

PS1='\[\033[38;5;172m\]\u\[\033[m\] in \[\033[38;5;172m\]\w\[\033[m\]'

and skip the function definition altogether.


Expansion: To get the PS1 variable to dynamically expand function calls, use a backslash to hinder premature evaluation:

PS1="$(ext-color 172 "\$USER") in $(ext-color 172 "\$PWD")"

should work.

share|improve this answer
    
The first two seem to work. However $PWD doesn't change when I cd. – Dave Kennedy May 4 '13 at 20:34
    
@davidkennedy85: Alright, I suspected that might be the case, but I wasn't sure. I'll update. EDIT: or you mean on all variants? Hmm, yeah, I can see why. I wonder how we can pass the \w to the external call... I didn't even look at this since your question specifically concerned the \u part :-) – Daniel Andersson May 4 '13 at 20:35
    
Odd - calling pwd from the command line or even echo $PWD displays /home/dave/Code but the prompt is not changing. – Dave Kennedy May 4 '13 at 20:40
    
@davidkennedy85: The pwd call in the PS1 variable will only be made once when the .bashrc file is read, so it won't continuously be updated. The \w sequence is meant to take care of this as a "magic value" that gets interpreted by internal Bash logic. – Daniel Andersson May 4 '13 at 20:44
    
That figures. I was just trying to be fancy. So are you saying it's impossible to send special bash characters to a custom function? – Dave Kennedy May 4 '13 at 20:46

You must log in to answer this question.

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