150

In my terminal prompt definition in my .bashrc file, among other things, I have this snippet of code:

${debian_chroot:+($debian_chroot)}

What does this do, and do I need it?

0

4 Answers 4

129

The important part to answer this question is this snippet from /etc/bash.bashrc:

if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

It means if the variable $debian_chroot is empty and the file /etc/debian_chroot exists and is readable the variable is set to the content of the file.

Now what is this for? The file /etc/debian_chroot is when you have a chrooted debian system inside another debian system (ubuntu is based on debian). So this is for a better overview. To distinguish whether you are in the chroot or not.

When you have a chroot of another system for example in /srv/nfs4/netboot/ you can set a name for this chroot in /srv/nfs4/netboot/etc/debian_chroot (in my case it's a nfs4 pxe netboot drive):

user@host:~# echo "netboot" >/srv/nfs4/netboot/etc/debian_chroot

And then when you chroot inside:

chroot /srv/nfs4/netboot/

Your prompt looks like this:

(netboot)user@host:~#
85

Generally, ${var:+value} means:

if $var is defined and not null; then use 'value'; else do nothing

The debian_chroot variable is defined in /etc/bash.bashrc file. It takes the content of /etc/debian_chroot file if this file exists and is readable. By default this file doesn't exists.

For more details, see:

Now, to understand better what exactly it is happening there, do the following in terminal:

radu@Radu:~$ PS1='${var:+($var)}\u@\h:\w\$ '
radu@Radu:~$ var="test"
                  ----
                   |
  ------------------
  |
  V
(test)radu@Radu:~$ var=""
radu@Radu:~$ var="and so on"
(and so on)radu@Radu:~$
0
21

If the environment variable $debian_chroot exists and is not empty ${debian_chroot:+($debian_chroot)} is replaced by ($debian_chroot) (that is the value of $debian_chroot with parens around it).

$debian_chroot is set in /etc/bash.bashrc to the contents of /etc/debian_chroot if that file exists (it doesn't by default) and $debian_chroot doesn't have a value yet.

${debian_chroot:+($debian_chroot)} is usually used to define your Bash prompt, for example

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

As the name suggests you can use this variable to indicate which chroot you are in by placing etc/debian_chroot into your chroot root folders.

If you don't know what a chroot is chances are you don't need that ;-) But you still may abuse it to include some other information into your Bash prompt

By default it doesn't do anything.

0

If you never need to use debian_chroot then it's a handy place to put the time the command prompt was displayed by using:

export PROMPT_COMMAND='debian_chroot=$(date +%r)'

Type this in your terminal and watch your command prompt change with the time:

rick@alien:~$ export PROMPT_COMMAND='debian_chroot=$(date +%r)'

(09:14:59 PM)rick@alien:~$ 

After the time is set once, to get a running clock which updates every second use:

while sleep 1;do tput sc;tput cup $(($(tput lines)-1)) 1;printf `date +%r`;tput rc;done &
4
  • 2
    While this works, it's odd. Why not use a more appropriate variable name in a custom $PS1?
    – Adam Katz
    Dec 9, 2019 at 23:02
  • @AdamKatz It doesn't work when a line overflows terminal width and wraps around. Then you can't recall it properly with up arrow. I was actually meaning to fine tune it but ran out of time. I'm not sure what you mean by "more appropriate variable name"? Dec 10, 2019 at 0:13
  • Safe wrapping requires adding \[ and \] (or \001 and \002) around control characters. The default bash prompt in Debian does this, but your tput commands likely break it. This has nothing to do with my comment, in which I propose using a separate variable in the definition of $PS1 in your ~/.bashrc
    – Adam Katz
    Dec 10, 2019 at 16:16
  • If you want to add the time to your prompt, consider export PS1="(\t)$PS1" with \t or \@ as noted in the PROMPTING section of the bash(1) man page. Note, this can't do strftime strings like %r, so you could alternatively do export PS1="($now)$PS1" PROMPT_COMMAND='now=$(date +%r)' which will preserve the Debian chroot indicator.
    – Adam Katz
    Dec 10, 2019 at 16:25

You must log in to answer this question.

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