Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am running an Ubuntu VM out of Virtual Box and Vagrant:

vagrant@lucid64:~$ uname -a
Linux lucid64 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux

My $PS1 variable doesn't actually match the prompt and is far more complex:

vagrant@lucid64:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

I am looking for an explanation of the relationship between the $PS1 and my prompt, which is $USER@$HOST:$PWD\$, as can be seen above.

E.g., on my regular laptop, which hosts this VM and is a Mac, it is clean and simple as follows:

~@12:10:20>echo $PS1
\w@\t>
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

The default $PS1 in Ubuntu consists of three parts:

  • \[\e]0;\u@\h: \w\a\]

    This is an escape sequence which will set the terminal title text to $USER@$HOST: $PWD.

    \[ and \] indicate the beginning and end of a sequence of non-printing characters.

    \e is an ASCII escape character.

    ]0; is the specific escape sequence to set the terminal icon and title in xterm compatible terminals

    \u expands to the username of the current user.

    @ is a literal @.

    \h expands to the hostname.

    : is a literal colon character.

    \w expands to the current working directory.

    \a is an ASCII bell character.

  • ${debian_chroot:+($debian_chroot)}

    If you're in a chroot environment, this will expand to the name of the chroot in parentheses.

    ${var:+OTHER} evaluates to $OTHER if var is set, otherwise as null string. $debian_chroot is a variable initalized in /etc/bash.bashrc to the contents of the file /etc/debian_chroot. Thus if your chroot environment includes this file, the prompt will include the contents of that file as an indication for which chroot the shell currently operates in.

  • \u@\h:\w\$

    This is the actual prompt you typically see.

    \u, @, \h, :, \w are as above.

    \$ expands to a number sign # if the effective uid is zero (i.e. user is root), otherwise it expands to a dollar sign $.

Resources

share|improve this answer
add comment

It's probably easiest to break this down!

In this first section:

\[\e]0;\u@\h: \w\a\]

The first part of this (the \[\e]0; bit) contains terminal control characters (often used for colours in the bash prompt and updating window titles, among other things - I'll explain more further down.)

The second part (the \u@\h:) will give you the user@host part of the prompt, while the \w will echo the path of the current working directory. The \a is an ASCII bell character.

A quick search revealed that, essentially, that part of your $PS1 is to tell bash to update the title of the terminal window to the same value as your prompt like this. (I know the shell pictured is zsh, but the same principle applies.) Essentially, that entire first 'phrase' is to update the current window title.


${debian_chroot:+($debian_chroot)}

The part above simply means that "if the variable $debian_chroot is defined (i.e. not null), then include that string in the prompt". I'm fairly sure that the variable $debian_chroot only ever carries a value when the system's being chrooted into. More info here.


\u@\h:\w\$

This last section creates the standard user@host: ~/$ prompt.

HTH

Edit: whoops, seems like somebody beat me to it.

share|improve this answer
add comment

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.