What does “${debian_chroot:+($debian_chroot)}” do in my terminal prompt?

1
The information in this thread has been moved to https://help.ubuntu.com/community/CustomizingBashPrompt

A thread for discussion of the wiki page only can be found here http://ubuntuforums.org/showthread.php?t=2012407

Thread closed.

If you have difficulties using the terminal because the prompt isn't visible enough or you would simply like it to look nicer, this howto is for you. It should work on any distribution if you use bash shell, just don't expect your existing .bashrc file to look like Ubuntu's. I've attached a before and after picture of my terminal as an example of what you could do.

So let's get started. Fire up your terminal and stay in your home directory.

1. Backup

First, let's backup our .bashrc file since we're about to make some changes to it:

Code:

cp .bashrc .bashrc-backupThe .bashrc file tells bash what to do when terminal is started, including how to show prompt.

2. Preparing the .bashrc

Use your favorite...

0 0
2

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?

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):

[email...
0 0
3

Hi All,

LMDE novice user.

Received prompt from update manager to update bash from 4.2+dfsg-1 to 4.3-9.2 which I duly did.

However now when I open terminal I have lost text colour schemes for prompts. Used to be "user@host:" in bold neon green followed by "~$" in bold sky blue. Folder path navigation was always in light blue too. Found it very useful and aesthetically pleasing but now its just white text and hard to eyescan.

Did a search and have found various articles on how to customise colour schemes in .bashrc but I can't get it to work properly. The unedited line since update is:

PS1="\[\e]0;\${debian_chroot:+($debian_chroot)}\u@\h: \w\a\] $PS1

Also wondering why this has changed after update. I believe my colour scheme was always default. The only thing I set away from default was the background colour.

Can anyone shed some light?
...

0 0
4

Discussion of this wiki can be found here

If you have difficulties using the terminal because the prompt isn't visible enough or you would simply like to customize its appearance, this howto is for you. It should work on any distribution utilizing the bash shell.

Fire up your terminal and stay in your home directory.

Backup

First back up your default .bashrc file, so you can always fall back on it should you mess something up.

cp .bashrc .bashrc-backup

The .bashrc file defines many aspects of the shell environment, including showing the prompt.

Preparing .bashrc

Use your favorite text editor to edit the original .bashrc file, eg.: gedit .bashrc

Locate this if/then statement:

if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi

Bash reads PS1 variable to define the primary...

0 0
5

Change the \w (lowercase) to \W (uppercase):

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ ' ^^ this one waaaaaay over here ------------------------------------------------+

Have a look at the Bash Prompt HOWTO for lots of fun details. example:

user@host:/usr/local/bin$ echo $PS1 ${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ user@host:/usr/local/bin$ export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\W\[\033[00m\]\$ ' user@host:bin$

The PROMPT_COMMAND variable, if set, is a command that gets run before displaying the prompt specified in PS1. In your case, PROMPT_COMMAND runs an echo statement with certain ANSI escape sequences that manipulate the titlebar of an Xterm.

If you suspect your PROMPT_COMMAND is overriding your PS1 prompt, you can unset it and test...

0 0
6

Well, I renamed .bashrc (in order to keep it as a backup) in my home folder and then I got the bash-3.2$ prompt. For some reason, the PS1 in /etc/profile.bash is not taken.

If I just su, I got bash-3.2#. If I "su -", I got [root@myhost ~]#. Hypothesis: /etc/profile.bash is only observed by the root environment. May this be right?

So I edited the .bashrc that Debian had left, and I had to place it both as /home/user/.bashrc and /root/.bashrc. I copy it at the end. See that I made the prompt coloured when being root.

Still, if I did "su -" or "su username -", or logged-in in text-mode as root, I got the profile that is on /etc/profile.bash So I renamed /etc/profile.bash and copied that same file /root/.bashrc as /etc/profile.bash. I hope not to have committed any insanity , otherwise please let me know.

Thank you.

# ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for...
0 0
7

Да, забыл. Вот bashrc полностью

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups
# ... and ignore same sucessive entries.
export HISTCONTROL=ignoreboth

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color,...

0 0
8
Try cd /

Your prompt will change from a ~$ at the end to a /$ at the end. Believe it or not, that's the directory you're currently in.

If you're in the /etc/apt directory, your prompt will look like username@username-/etc/apt$

The reason you see a ~ is because ~ is a shortcut to your home directory. On most linux systems, you'll find it at /home/username.

In response to the above commenter: Normal users have read-only access to most of the system, with the exception of sensitive files like password lists or SSH private keys. You can look around quite safely as a normal user without hurting anything.

How do you know whether you're a normal user or not? If your prompt ends in a $, then you're a normal user; if your prompt ends in a # then look out because you're working as root.

Oh, about that: Root is the name of the admin account on Linux. He can do anything he wants to his computer including completely breaking it with a single command. Root's...

0 0
9

That's odd. I don't have a .bashrc file. Shall I create and add that code to the .bashrc?

I have not altered any files on my system other than doing a apt-get update && apt-get upgrade.

you probably know this but .bashrc is a hidden file, so you can't see it until you do a CTRL-H or view -show hidden i think in PcManFM.

You should have one and you should also have a .profile file. below is the code found in .bashrc on a default install. I alter mine all the time so I had to get one off of a live cd because mine is highly customized.

# ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for examples # If not running interactively, don't do anything [ -z "$PS1" ] && return # don't put duplicate lines in the history. See bash(1) for more options # ... or force ignoredups and ignorespace HISTCONTROL=ignoredups:ignorespace # append to the history file, don't overwrite it shopt -s...
0 0
10

Let's say I'm on a fresh install of Debian, Jessie (or Wheezy). As a regular user, I've got the prompt defined in the /etc/bash.bashrc file, eventually override by the one in my /home/foobar/.bashrc

Ok, cool, so I've got something like that: [email protected]:~$ Now, I'm trying to understand the code below:

# If this is an xterm set the title to [email protected]:dir case "$TERM" in xterm*|rxvt*) PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\[email protected]\h: \w\a\]$PS1" ;; *) ;; esac

As you can see, the PS1 variable (= the prompt) seems to be overrided again (cause yeah I'm in a xterm). But I can't get why we have another $PS1 on the end of the line!

In order to get this, I've put 3 letters a b c in this line :

PS1="a\[\e]0;${debian_chroot:+($debian_chroot)}\[email protected]\h: \w\a\]b$PS1 c"

And now, I have the strange prompt below:

[email protected]:~$ c

So it seems that the almost entire line is not use, everything between the...

0 0
11

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:

...
0 0
12

Chroot is a unix feature that lets you restrict a process to a subtree of the filesystem. One traditional use is FTP servers that chroot to a subset of the filesystem containing only a few utilities and configuration files, plus the files to serve; that way, even if an intruder manages to exploit a bug in the server, they won't be able to access files outside the chroot. Another common use is when you're installing or repairing a unix system and you boot from a different system (such as a live CD): once a basic system is available, you can chroot into it and do more work.

The prompt setting includes the content of $debian_chroot in the prompt, inside parentheses, unless it is empty. This variable is initialized in /etc/bash.bashrc to the contents of the file /etc/debian_chroot. Thus, if you follow the convention to include a name for your chroots at the location /path/to/chroot/etc/debian_chroot, your prompt will contain an indication of which chroot you're in. A program...

0 0
13

Introduction

As you manage Linux servers, you'll spend quite a bit of time using the command line. For most people, this means spending a lot of time with the Bash shell.

While most distributions provide sensible defaults for the styling of user and root prompts, it can be helpful to customize your prompt to add your own preferences. You can include a lot of useful information that can help you stay oriented and remind you when you are operating with elevated privileges.

We will be using an Ubuntu 12.04 VPS to experiment, but almost all modern Linux distributions should operate in a similar manner.

Verify that your Shell is Bash

Before we begin to actually customize the shell, you should verify that your current shell actually is Bash.

This should be true for the vast majority of systems, but sometimes distribution maintainers opt for a different shell or users test out a new shell.

It is easy to verify by checking the /etc/passwd...

0 0
14
...
0 0
15

Most Linux distributions configure the Bash prompt to look something like username@hostname:directory$ . But you can configure the Bash prompt to contain whatever you like, and even choose whatever colors you like.

The example steps here were performed on Ubuntu 16.04 LTS. The process should be the same on other Linux distributions, although the default Bash prompt and settings in the .bashrc file may be a bit different.

Where the Prompt Variable is Stored

Your Bash prompt configuration is stored in your user account’s .bashrc file, which is at ~/.bashrc. So, if your username is bob, the file is at /home/bob/.bashrc.

You can open the file to view the current Bash variable. We’ll use nano as our example text editor, although you could also use vi, emacs, or any other text editor you’re comfortable with. Open a Terminal and run:

nano ~/.bashrc

Scroll down to the PS1= section. The first variable looks rather complicated because it...

0 0
16


Hmmm. I don't see the LF before the \$ in either of those definitions, nor did the colored one result in the prompt spanning two lines for me. Is something else setting PS1 perhaps?

to see the running definition. There should be a \n before the '\$ '

Then search .bashrc, .bash_profile, /etc/bash* files for that definition and remove the \n

Hi BRD.

No, I want everything on one line - including the blinking cursor.

Hi scasey.

Here's the output...

Code:

\[\e[1;35m\]\u\[\e[0m\]@\[\e[1;36m\]\H\[\e[0m\]:\[\e[1;32m\]\w$\n\[\e[1;32m\]$\[\e[0m\]

Remove the highlighted \n and the prompt will be all on one line.

Can't tell you where to do that tho...you'll need to grep for PS1 in the various bash files: ~/.bashrc, ~/.bash_profile, /etc/bash* and change the one that looks like the output of the echo.

Then, do what...

0 0
17
# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
#[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the...

0 0
18

Best guess for default Ubuntu install

Find where your PS1 variable is set and change \w to \W.

You can do an initial check of this method thus:

user@computer:~/full/path/to/directory$ echo $PS1 ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ user@computer:~/full/path/to/directory$ export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ ' user@computer:directory$

It is probably being set in your .bashrc. If not, check /etc/bashrc and override the variable there in your .bashrc. You will of course have to do an exec bash or source your .bashrc for changes made there to take effect.

Other setups

There are different variations on how to do this depending on what shell you are using and how it is set up. For example, you might conceivably have your prompt set up like this:

export PS1='$USER@$(hostname):$PWD$ '

In which case you will want...

0 0
19

1)first of all - backup your .bashrc file

cp .bashrc .bashrc-backup

2)now edit .bashrc with your editor:

gedit .bashrc

search for lines that contain the string: PS1
and comment them (with # before them).

in the end of the file add:

PROMPT_HOSTNAME='SET_HERE'
PROMPT_COLOR='1;37m'
PROMPT_COLOR2='1;32m'

PS1='\e[${PROMPT_COLOR}[\e[${PROMPT_COLOR2}\u@${PROMPT_HOSTNAME}\e[${PROMPT_COLOR}] $ '

the first color is for the normal font and barckets, and the second one is for the user name and host

I choosed green and white. you can change the first number to any of the following:

30: Black/Dark grey
31: Red
32: Green
33: Yellow
34: Blue
35: Magenta
36: Fuscia
37: White/light grey
38: "Default" foreground color

in the exmaple "1;37m" you will have to change the 37 to another number.
the 1 before it can be set into zero, it is for light \ dark...

0 0