nixCraft Poll

Topics

How to: Change / Setup bash custom prompt (PS1)

Posted by Vivek Gite [Last updated: October 9, 2007]

So how do you setup, change and pimp out Linux / UNIX shell prompt?

Most of us work with a shell prompt. By default most Linux distro displays hostname and current working directory. You can easily customize your prompt to display information important to you. You change look and feel by adding colors. In this small howto I will explain howto setup:
a] Howto customizing a bash shell to get a good looking prompt
b] Configure the appearance of the terminal.
c] Apply themes using bashish
d] Howto pimp out your shell prompt

Prompt is control via a special shell variable. You need to set PS1, PS2, PS3 and PS4 variable. If set, the value is executed as a command prior to issuing each primary prompt.

How do I display current prompt setting?

Simply use echo command, enter:
$ echo $PS1
Output:

\\u@\h \\W]\\$

How do I modify or change the prompt?

Modifying the prompt is easy task. Just assign a new value to PS1 and hit enter key:
My old prompt --> [vivek@105r2 ~]$
PS1="touch me : "
Output: My new prompt

touch me : 

So when executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:

Let us try to set the prompt so that it can display today’d date and hostname:
PS1="\d \h $ "
Output:

Sat Jun 02 server $ 

Now setup prompt to display date/time, hostname and current directory:
$ PS1="[\d \t \u@\h:\w ] $ "
Output:

[Sat Jun 02 14:24:12 vivek@server:~ ] $

How do I add colors to my prompt?

You can change the color of your shell prompt to impress your friend or to make your own life quite easy while working at command prompt.

Putting it all together

Let us say when you login as root/superuser, you want to get visual confirmation using red color prompt. To distinguish between superuser and normal user you use last character in the prompt, if it changes from $ to #, you have superuser privileges. So let us set your prompt color to RED when you login as root, otherwise display normal prompt.

Open /etc/bashrc (Redhat and friends) / or /etc/bash.bashrc (Debian/Ubuntu) or /etc/bash.bashrc.local (Suse and others) file and append following code:
# vi /etc/bashrc
or
$ sudo gedit /etc/bashrc
Append the code as follows

# If id command returns zero, you’ve root access.
if [ $(id -u) -eq 0 ];
then # you are root, set red colour prompt
  PS1="\\[$(tput setaf 1)\\]\\u@\\h:\\w #\\[$(tput sgr0)\\]"
else # normal
  PS1="[\\u@\\h:\\w] $"
fi

Close and save the file.

My firepower prompt

Check this out:
Firepower shell prompt using bashish
(click to enlarge)

You can also create complex themes for your bash shell using bashish. Bashish is a theme enviroment for text terminals. It can change colors, font, transparency and background image on a per-application basis. Additionally Bashish supports prompt changing on common shells such as bash, zsh and tcsh. Install bashish using rpm or apt-get command:
# rpm -ivh bashish*
OR
# dpkg -i bashish*
Now start bashish for installing user configuration files:
$ bashish
Next you must restart your shell by typing the following command:
$ exec bash
To configure the Bashish theme engine, run
$ bashishtheme

basish in action (screenshots from official site):
flower.png

urbandawn - based on an artwork by grevenlx
Finally, you can always use aterm or other terminal program such as rxvt. It supports nice visual effect , like transparency, tinting and much more by visiting profile menu. Select your terminal > click on Edit menu bar > Profiles > Select Profile > Click on Edit button > Select Effects tab > Select transparent background > Close

Linux desktop nice visual effect , like transparency, tinting etc
(click to enlarge)

Further readings:

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. akashdeep Says:

    LOL .. nice your command is my wish

  2. Kirk Strauser Says:

    Here’s my ZSH prompt. I start by defining a bunch of complicated variables, then actually build the prompt with a much more legible set of instructions.


    # Define common and useful things to put in a prompt
    typeset -A prc
    prc[abbrevpath]=’%{${fg[red]}%}%B%45

    The result is like:


    kirk@mysystem [7:54} ~
    :) $

    where “kirk@mysystem” is green for normal users and red for root. The time is blue. The current directory is in red, and if it’s longer that a set amount, the beginning is replaced with ” … ” (so that you always see bottommost directory you’re in). The smiley face is “:)” in green if the last command you typed executed correctly, or “:(” in red if it didn’t. “$” is black, and if you’re root, it’s “#” instead.

    It packs a lot of useful information into a small area, like who you are, where you are, whether you’re root, and whether you can make commands that succeed. If I see too many red frowns, I call it quits for the day.

    I think you can do most of this in Bash, too, but I never got that creative.

  3. SimpleSimon Says:

    Two things that I’d like to know how to do (if possible) with my bash prompt…

    1) Include the full path of the current directory, but only the last 25 characters if the path is longer than that. I’d also prefer a shortened path to be preceeded by “…”.

    2) Memorize the current cursor position, drop to the bottom line of the screen, output my prompt info, jump back to the previous cursor position.

    Either of these possible?

  4. chika Says:

    how to eliminate the hostname on the konsole?
    what should i add?

  5. chika Says:

    hey chika do this on your console

    PS1=”\d $”

  6. oscar Says:

    How to change the prompt when access is via ssh, I have tried and don’t find that setting.
    Debian

  7. Jamie Says:

    @ SimpleSimon

    For your first question, you want to look at the PROMPT_COMMAND variable. I don’t have an answer for your second question.

    Example:
    # This will limit the path to 30 characters.
    PROMPT_COMMAND=’if [ ${#PWD} -gt 30 ]; then myPWD=${PWD:0:12}…${PWD:${#PWD}-15}; else myPWD=$PWD; fi’
    PS1=”\u@\h \$myPWD\$ ”

    HTH,
    Jamie

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.