keepcalculator

06 Numbers and Powers

Useful and Interesting Bash Prompts

Card No. 06·49809 · Updated · Standard formula, shown below

This article explains how to customize your Bash prompt to show useful information like the current directory, git branch, and exit status, and how to add color and styling. You’ll learn the syntax, see practical examples, and find answers to common questions.

What Is a Bash Prompt?

The Bash prompt is the text that appears in your terminal when the shell is ready to accept a command. It’s defined by the environment variable PS1, which stands for “prompt string one.” Every time you press Enter, Bash re-evaluates PS1 and displays the result. The default prompt on many systems is something like user@host:~/directory$, but you can change it to show almost anything.

Customizing your prompt is not just cosmetic—it can save time and prevent mistakes. For example, seeing the git branch in the prompt tells you which repository you’re in without typing git status. Seeing the exit status of the last command helps you know if it succeeded at a glance.

How the Prompt Works

The PS1 variable is a string that can contain plain text and special escape sequences. When Bash displays the prompt, it replaces these sequences with their values. For example, \u becomes your username, \h becomes the hostname, and \w becomes the current working directory. The full list of escapes is in the Bash manual, but here are the most useful ones:

  • \u – username
  • \h – hostname (short)
  • \w – current working directory (full path)
  • \W – basename of the current directory
  • \d – date (e.g., Tue May 26)
  • \t – time in 24-hour format (HH:MM:SS)
  • \n – newline
  • \$ – # if root, $ otherwise
  • \e – escape character (for color codes)

You can also include command substitutions using $(command) inside the prompt. This is how you can show the git branch, the exit status, or any other dynamic information. However, be careful—running commands in the prompt can slow it down if they are slow (like git status in a large repository).

Customizing Your Prompt

To change your prompt, you set the PS1 variable in your ~/.bashrc file. For example, to show the username, hostname, and current directory in color, add this line:

export PS1='\[\e[32m\]\u@\h\[\e[0m\]:\[\e[34m\]\w\[\e[0m\]$ '

This makes the username and hostname green, the directory blue, and resets colors after each part. The \[ and \] tell Bash that the enclosed text is non-printing, so it can calculate the prompt width correctly (important for line wrapping).

If you want a newline after the prompt, add \n at the end. For example, many developers like a prompt that shows the time and directory on one line, and then a $ on the next line:

export PS1='\t \w\n$ '

Useful and Interesting Prompt Ideas

Here are some concrete examples that go beyond the default. You can mix and match these to create your own.

  • Show the exit status of the last command: Add \$? to your prompt. This will display the exit code (0 for success, non-zero for failure). For example: PS1='[\u@\h \w] \$?\$ '
  • Show the git branch: Use a command substitution to get the current branch. A common snippet is $(git branch 2>/dev/null | grep '\*' | cut -d' ' -f2). To make it appear in the prompt: PS1='\u@\h \w $(git branch 2>/dev/null | grep "\*" | cut -d" " -f2)\$ '
  • Show the Python virtual environment: If you use virtualenv, the VIRTUAL_ENV environment variable is set. You can show it with $(basename $VIRTUAL_ENV).
  • Color code the prompt based on root vs. user: You can use an if statement in your prompt to change color if you are root. For example: PS1='\[\e[31m\]\u\[\e[0m\]@\h:\w\$ ' would be red for the username, but you can make the whole prompt red if root with a more complex expression.
  • Show the history number: Use \! to show the command number in history. This is useful if you want to re-run a command with !number.
  • Show the time in a different format: Instead of \t, you can use \@ for 12-hour time with AM/PM, or \A for HH:MM without seconds.

One interesting prompt that many users enjoy is the “powerline” style, which uses Unicode characters and colored backgrounds to create a visually appealing prompt. While implementing it fully requires a patched font, you can get a similar effect with simple color blocks. For example, you can use \[\e[44m\] \w \[\e[0m\] to have a blue background behind the directory.

How to Choose What to Include

When designing your prompt, think about what information you frequently need. Here are some factors to consider:

  • Frequency of use: If you use git daily, show the branch. If you rarely check the date, omit it.
  • Clutter: Too much information makes the prompt long and hard to read. Aim for a balance.
  • Performance: Commands like git status can be slow in large repositories. Use git branch instead, which is faster, or cache the branch name.
  • Portability: If you use the same prompt on different systems, avoid hardcoding paths or using tools that may not be installed.

A good rule of thumb is to show the current directory, the exit status, and the git branch if you’re in a git repository. That covers most needs without being overwhelming.

Common Problems and Solutions

Here are a few issues you might run into when customizing your prompt:

  • Line wrapping is broken: This happens when you use color codes without wrapping them in \[ and \]. Always wrap non-printing sequences.
  • Prompt doesn’t update: If you change PS1 in a terminal, you need to run source ~/.bashrc or open a new terminal. Also, make sure you are editing the correct file (~/.bashrc for interactive shells, not ~/.bash_profile unless you know what you’re doing).
  • Command substitution is slow: If your prompt runs a slow command, every prompt display will lag. Use faster alternatives or cache the result.
  • Unicode characters display as boxes: This is usually a font issue. Install a font that supports the glyphs you use, like a powerline font.

Comparing Popular Prompt Styles

There are several well-known prompt styles you can adopt or adapt. The default prompt is simple and functional. The Git-aware prompt adds branch information. The Powerline prompt is visually rich but requires patched fonts. The “minimal” prompt shows only the current directory and a $ sign, which some users prefer for a clean look.

Each style has its trade-offs. The default is easy to read but lacks context. The Git-aware prompt is helpful for developers but can be slow. The Powerline prompt is beautiful but complex to set up. The minimal prompt is fast and clean but offers little information. You can mix elements from each to suit your needs.

Frequently Asked Questions

How do I make my prompt show the git branch?

Add a command substitution to PS1 that prints the branch name. A simple way is $(git branch 2>/dev/null | grep '\*' | cut -d' ' -f2). This will show nothing if you’re not in a git repository. For a more robust solution, you can use $(git rev-parse --abbrev-ref HEAD 2>/dev/null).

Why is my prompt not wrapping lines correctly?

This is almost always because you forgot to wrap color escape sequences in \[ and \]. These brackets tell Bash that the characters inside don’t take up space on the screen, so it can calculate the prompt length correctly.

How do I change the prompt permanently?

Add the export PS1='...' line to your ~/.bashrc file. Then run source ~/.bashrc or start a new terminal session. The change will apply every time you open a terminal.

Can I have different prompts for different directories?

Yes, you can use a function in your PROMPT_COMMAND variable to set PS1 based on the current directory. For example, you could check if you’re in a git repository and set a different prompt accordingly.

What is the difference between PS1 and PROMPT_COMMAND?

PS1 is the prompt string itself. PROMPT_COMMAND is a command that is executed before each prompt is displayed. You can use PROMPT_COMMAND to set PS1 dynamically, for example, to include the exit status of the last command.

How do I reset my prompt to the default?

Simply set PS1 back to the default value: export PS1='\u@\h:\w\$ '. Or remove the line from your ~/.bashrc and reload the file.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →