Linux

Customizing the bash prompt: configuring PS1, colors, and information

In this guide, you'll learn how to fully customize the bash prompt using the PS1 variable. We'll cover color settings, displaying system information and conditional constructs.

Updated at February 16, 2026
15-30 minutes
Medium
FixPedia Team
Применимо к:Bash 4.0+Ubuntu 20.04+Fedora 35+Arch Linux

Introduction / Why This Is Needed

The command-line prompt (bash prompt) is the string you see before the cursor in the terminal. By default, it usually looks like user@host:~/path$. Customizing your prompt makes working in the terminal more informative and visually pleasant. You can add the current time, the status of the last command, your Git branch, and use colors to highlight important information. After completing this guide, you will have a prompt that adapts to your tasks and preferences.

Requirements / Preparation

  1. You must have bash installed (version 4.0 or higher). Check the version: bash --version.
  2. Ensure you have access to your home directory and can edit the ~/.bashrc file.
  3. It is recommended to use a terminal that supports 256 colors (e.g., GNOME Terminal, Konsole, iTerm2). Check: echo $TERM (should be xterm-256color or screen-256color).
  4. Basic knowledge of editing text files in Linux (nano, vim, etc.).

Step 1: Find and Open the Bash Configuration File

Bash reads configuration files on startup. For interactive shells, this is typically ~/.bashrc (user-specific) or /etc/bash.bashrc (system-wide). We will edit the user file so changes don't affect other users.

Open your terminal and run:

nano ~/.bashrc

Or use your preferred editor (vim, code, gedit). If the file is empty or doesn't exist, create it.

Step 2: Understand the PS1 Variable Structure

The PS1 variable defines the primary prompt. It supports many escape sequences that are replaced with current data. Here are the main ones:

Escape SequenceDescription
\uCurrent username
\hHostname (up to the first dot)
\HFull hostname
\wFull path of the current directory, with ~ for the home directory
\WOnly the name of the current directory
\$# for root, $ for a regular user
\tCurrent time in 24-hour format (HH:MM:SS)
\TTime in 12-hour format (HH:MM:SS)
\@Time in 12-hour format with AM/PM
\dDate in "Day MM YYYY" format
\nNew line
\[ and \]Mark the beginning and end of non-printable characters (e.g., for colors)

Important: Colors use ANSI escape sequences, which must be wrapped in \[ and \] so bash correctly calculates the prompt length.

Step 3: Add Simple Colors and Information

Let's create a simple colored prompt that shows the user, host, and current directory.

Add this line to your ~/.bashrc:

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

Breakdown:

  • \[\e[0;32m\] — enables green color (code 32). \e is the escape character, [0;32m is the color code.
  • \u@\h:\w\$ — combination of escape sequences: user, @, host, colon, current path, $ or #.
  • \[\e[m\] — resets colors to default.
  • A space at the end for convenience.

After saving the file, run source ~/.bashrc or open a new terminal. You should see a green prompt.

Step 4: Use More Complex Colors and Formatting

You can combine colors and attributes (bold, underline). For example, for bold green text:

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

Here:

  • \[\e[1;32m\] — bold green (1 is bold, 32 is green).
  • \[\e[m\] — reset after the username and host.
  • \[\e[1;34m\] — bold blue for the path.

Color list (add 1; before the code for bold):

  • Black: 30
  • Red: 31
  • Green: 32
  • Yellow: 33
  • Blue: 34
  • Magenta: 35
  • Cyan: 36
  • White: 37

You can also use the 256-color palette: \[\e[38;5;{number}m\] (e.g., \[\e[38;5;202m\] for orange). Color number from 0 to 255.

Step 5: Add Dynamic Information (Time, Status, Git)

Time

To add the current time, use \t or \@:

PS1='\[\e[0;33m\]\t \[\e[m\]\u@\h:\w\$ '

Last Command Status

The escape sequence \$? returns the exit code of the last command (0 for success, non-zero for error). But it doesn't work directly in PS1. You need to use command substitution or a conditional construct. Example to show ! on error:

PS1='\[\e[0;31m\]$(if [[ $? != 0 ]]; then echo "! "; fi)\[\e[m\]\u@\h:\w\$ '

However, this can slow down your prompt because it runs for every prompt display. A more efficient method is to use PROMPT_COMMAND:

PROMPT_COMMAND='RET=$?; [[ $RET != 0 ]] && echo -n "!$RET "'
PS1='\[\e[0;33m\]\u@\h:\w\$ \[\e[m\]'

Git Information

If you work with Git, it's useful to show the current branch. Add a function to your ~/.bashrc:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

Then use it in PS1:

PS1='\[\e[0;36m\]\u@\h\[\e[m\]:\[\e[1;34m\]\w\[\e[0;33m\]$(parse_git_branch)\[\e[m\]\$ '

This function will show the branch in parentheses if the current directory is a Git repository.

Step 6: Optimize and Combine

Now let's combine everything: colors, time, Git, status. Example of an advanced prompt:

# Git function
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# PROMPT_COMMAND for status
PROMPT_COMMAND='RET=$?; [[ $RET != 0 ]] && echo -ne "\e[0;31m!$RET \e[m"'

# The PS1 itself
PS1='\[\e[0;33m\]\t \[\e[m\]\u@\h:\[\e[1;34m\]\w\[\e[0;32m\]$(parse_git_branch)\[\e[m\]\$ '

This prompt:

  • Shows the time (yellow).
  • Username and host (default color).
  • Current path (bold blue).
  • Git branch (green).
  • On error from the previous command, displays !code in red.

Tip: If the prompt becomes too long or slow (e.g., due to frequent git calls), simplify it. For Git, you can use faster alternatives like __git_ps1 from the git package (if installed).

Verifying the Result

  1. After making changes to ~/.bashrc, run source ~/.bashrc or close and reopen your terminal.
  2. Ensure the prompt displays with colors and information.
  3. Test scenarios:
    • Navigate to a directory with a Git repository — the branch should appear.
    • Run a command that fails (e.g., ls non_existent_file). The next prompt should show the error code.
    • Check that colors don't "bleed" (after the reset \[\e[m\], the following text should be the default color).
  4. If something doesn't work, check the syntax in ~/.bashrc (extra quotes, missing \[/\] for colors).

Common Issues

Issue 1: Colors Don't Display or "Bleed"

Cause: Incorrect use of \[ and \] around non-printable characters (escape sequences). Bash uses these markers to correctly calculate prompt length. Without them, the cursor can misalign.

Solution: Ensure each color block (including the reset) is wrapped in \[ and \]. Example of correct usage: \[\e[0;32m\]...\[\e[m\].

Issue 2: Prompt Is Too Slow

Cause: Running heavy commands (e.g., git status or parse_git_branch without caching) on every prompt display.

Solution:

  • Simplify the Git function by using __git_ps1 (if the git package is installed). It's optimized.
  • Or add caching: for example, update Git info only when the directory changes (via PROMPT_COMMAND and checking $PWD).
  • Remove unnecessary elements from PS1.

Issue 3: Unicode Symbols or Emojis Display Incorrectly

Cause: The terminal or font doesn't support these symbols, or the locale isn't UTF-8.

Solution:

  • Install a font with emoji support (e.g., Nerd Fonts).
  • Check the locale: locale should include UTF-8. If needed: export LANG=en_US.UTF-8 in ~/.bashrc.
  • Ensure your terminal is set to UTF-8.

Issue 4: Changes Don't Apply After source ~/.bashrc

Cause: A syntax error in ~/.bashrc (e.g., an unclosed quote). Bash may fail to load the file on error.

Solution: Check the file for syntax by running bash -n ~/.bashrc. Fix any errors. Also, ensure you aren't overwriting PS1 later in the same file (order matters).

F.A.Q.

How to restore the default bash prompt?
Why aren't colors working in my bash prompt?
How to add the current directory to the prompt?
Can I use Unicode symbols in the bash prompt?

Hints

Open the ~/.bashrc file
Find the PS1 variable
Modify PS1 to suit your needs
Apply the changes
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community