Bash Shell Prompt: (Linux/Mac) – How to shorten your bash prompt

I love using Terminator/iTerm2 rather then standard OS terminals, and love to to be able to run multiple terminals in a single window, and be able to run multiple commands at once. While these tools are awesome, they also mean that you are left with very small screen real estate for the terminal.  Tying long commands can be really tedious, and its not easy to read stuff when its wrapping to next line.

Bash prompt takes generally 20% of the screen space in your terminal.  Looking clearly, we know that It has some information that might rarely change.  When i am working on my local laptop, i already know who i am, and where i am. So this information is something that can be easily removed from bash prompt.  Following is a screenshot from default bash prompt from my mac ox machine.

full_prompt

Well my first option is to just show my username, and current working directory.  For that you can set following variable in your .bash_profile file.

PS1='\[33[01;34m\]\u \[33[01;31m\] \w > \[33[m\] '

Here \u stands for username, and \w stands for current working directory. Username is being shown so that its easy to differentiate between sudo vs non-sudo terminal session. These values are color coded so that its easy to differentiate. Here i am using \[33[01;34m\]  for username, and \[33[01;31m\]  for directory. This would convert your prompt to following

shorter_prompt

I personally like to shorten it further, and use following prompt instead.

PS1='\[33[01;31m\] \w > \[33[m\] 

I just like to show my current working directory, and not the username. This gives me more real estate.

shortest_prompt

To differentiate the sudo vs non-sudo, you can either set different color codings by using following small IF condition in your /etc/bashrc

if [[ ${EUID} == 0 ]] ; then
        #sudo user show in blue color
        PS1='\[33[01;34m\] \w> \[33[m\]'
else
        #non-sudo user show in red color
        PS1='\[33[01;31m\] \w> \[33[m\]'
fi

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s