Saturday, September 5, 2015

Customize bash prompt

Navigating in a Linux using shell can be a bit tedious, especially considering user sometimes will not be aware of his/her current working directory.

some of the examples of bash prompts looks like below.
$
#
bash::$
sh:$
sh:#
etc etc etc.

each of these prompts can be customized to provide a more meaningful prompt, an example shown below
[break-it@foo-bar-host ~]$ 
  break-it      - Current User Name
 foo-bat-host  - host name
  ~            - current working directory
on a whole translates to break-it is now logged on to system foo-bar-host working under home directory ( ~ is nothing but user`'s HOME )
Below are a few steps needed to customize bash prompt

Step-1: Login to terminal which probably looks like this.
$
$
Step-2:  lets start modifying type below lines on bash prompt and press enter
$
$ PS1="my-bash-$ "
my-bash-$
my-bash-$ 
There you go the new bash prompt. The trick here is to provide what ever you want as prompt to PS1 variable. for instance lets modify the prompt a bit more.
my-bash-$
my-bash-$ PS1='Hey, `whoami` Type-in your command Here # '
Hey, break-it Type-in your command Here #
Hey, break-it Type-in your command Here #
or change to this
$
$
$ PS1='[`whoami` `date +%F`] #'
[break-it 2015-09-06] #
[break-it 2015-09-06] #
or this

$$ PS1='[`whoami` `date +%F`] #'[break-it@foo-bar-host ~] #

Step-3:   Finally once done on selecting bash prompt copy paste the lines in to ~/.bashrc file.
            Here is a example of bashrc file contents.
myname=`whoami`
myhost=`hostname`

export PS1='[$myname@$myhost `pwd`]$ '

That's all you now have a customized bash prompt ready.


No comments:

Post a Comment