3

I'm seeing this in my .bashrc file:

 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\
      [\033[01;34m\]\w\[\033[00m\]\$ '

and I have absolutely no idea what all those escapes codes mean.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
user541686
  • 3,083

1 Answers1

10

There are three kinds of escape codes in there: bash parameter expansion, bash prompt expansion, and terminal escape codes.

  • ${debian_chroot:+($debian_chroot)} means “if $debian_chroot is set and non-empty, then ($debian_chroot), else nothing”. (See /etc/bash.bashrc for how debian_chroot is defined. As the name indicates this is a Debian thing.)
  • The backslash escapes are prompt escapes. \u is replaced by the user name, \h is replace by the machine name, and so on (see the manual for a list). Parts within \[…\] are terminal escapes; the brackets tell bash that these parts don't take any room on the screen (this lets bash calculate the width of the prompt). \033 is the ESC character (character number 033 octal, i.e. 27 decimal, sometimes written \e or ^[); it introduces terminal escape sequences.
  • ESC [ codes m (written CSI Pm m in the xterm control sequences list) changes the color or appearance of the following text. For example the code 1 switches to bold, 32 switches the foreground color to green, 0 switches to default attributes.
  • 3
    The ESC codes are known as ANSI Escape Sequences. You can find a lot about them on the net. – Mark Jul 19 '11 at 19:41
  • +1 Thanks for the explanations! :) @Mark: That's really useful, thanks for pointing it out! I had no idea what to call them before. :) – user541686 Jul 19 '11 at 20:14