CodeSpud

Make Your Bash Prompt Work For You

November 14, 2016

bashproductivitycli

If you work with Linux or Unix-like operating systems like Ubuntu or the MacOS, you might be familiar with Bourne-Again Shell or BASH for short. This article will show you that with a little elbow grease, you can have your BASH prompt work for you and heck maybe have a little fun with it.

People tell me the invention of the GUI (graphical user interface) is the best thing that ever happened to modern computing. I beg to differ. Not all work is accomplished through the GUI nor is it efficient.

I find myself working on the command line for work more and more — managing my servers even my Apple computer. So I get to spend much of my time on the command line. Launching commands like a stenographer recording the latest court drama. I realized very early how powerful the command line could be.

But not having a GUI does not mean you should be happy with the default command prompt. Is this familiar?

boring

Boring command prompt!

Using the table below we can build a BASH prompt that is not only pretty but saves us a few keystrokes as well.

BASH Special Characters Reference

Special character Description Special character Description
\a an ASCII bell character (07) \d the date in “Weekday Month Date” format (e.g., “Tue May 26”)
\] end a sequence of non-printing characters \e an ASCII escape character (033)
\h the hostname up to the first `.’ \H the hostname
\j the number of jobs currently managed by the shell \l the basename of the shell’s terminal device name
\n newline \r carriage return
\s the name of the shell, the basename of $0 (the portion following the final slash) \t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format \@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format \u the username of the current user
\v the version of bash (e.g., 2.00) \V the release of bash, version + patchelvel (e.g., 2.00.0)
\w the current working directory \W the basename of the current working directory
\! the history number of this command \# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number nnn
\\ a backslash \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\D{format} the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required

Change the Bash Prompt

The environment variable we want to modify is PS1.

editps1

Using the reference table above we can break down the current prompt as

  • \u – the current user
  • \h – the computer’s hostname
  • \w – the current working directory

Not very useful is it? Let’s change that.

By modifying the PS1 variable and exporting it with the command below we can manipulate the prompt to output anything we want.

bash_prompt_smiley

Add a smiley

Below is my favorite bash prompt which I install for all my servers and even my workstation. I can easily see important details like the hostname – very important if you find yourself managing multiple terminal windows and you can distinguish between windows. This configuration also shows the load averages, the current date and time and the working directory. The color helps to give it contrast to the monotonous black on white default of most terminal windows.

mybashpromot_in_action

Make it Permanent

To make your BASH prompt permanent, just edit ~/.bash_profile or ~/.bashrc or ~/.profile or run the command

Make your bash prompt permanent

Ideas

  1. Count the files in the current directory
  2. Display in blinking colors the last line of a todo list
  3. Display CPU/Memory/Disk usage
  4. Display command to pull up a help screen
  5. Echo a smiley if the system is working in top condition

There you have it, you can now work on the command line with a little bit more help. The key here is to be creative and think of what you want your bash prompt to have. Enjoy!

By @codespud  
DISCLAIMER This is my personal weblog and learning tool. The content within it is exactly that – personal. The views and opinions expressed on the posts and the comments I make on this Blog represent my own and not those of people, institutions or organisations I am affiliated with unless stated explicitly. My Blog is not affiliated with, neither does it represent the views, position or attitudes of my employer, their clients, or any of their affiliated companies.