Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Customize the Linux Bash Prompt

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Customizing your Linux bash prompt makes your terminal show exactly the information you need—like the current directory, git branch, or time—so you work faster and make fewer mistakes. You can change it permanently by editing the PS1 variable in your ~/.bashrc file. Here’s how to do it step by step.

What You'll Need

  • A Linux system with the bash shell (default on most distributions).
  • A terminal emulator (like GNOME Terminal, Konsole, or xterm).
  • A text editor (nano, vim, or gedit).
  • Basic familiarity with the command line.

Step-by-Step

  1. Open your terminal. You’ll see the default prompt, usually something like user@host:~$.
  2. Check the current PS1 value. Type echo $PS1 and press Enter. You’ll see the escape sequences that define your prompt, like \u for username, \h for hostname, and \w for current directory.
  3. Edit your .bashrc file. Run nano ~/.bashrc (or use vim ~/.bashrc). This file runs every time you start a new bash session.
  4. Find the PS1 line. Look for a line that starts with PS1=. If you don’t see one, you can add it at the bottom of the file.
  5. Create your custom prompt. Replace the PS1 line with something like:
    PS1='\u@\h:\w\$ '
    This shows username, hostname, current directory, and a dollar sign (or hash for root). You can use any combination of the following common codes:
    • \u – username
    • \h – hostname (short)
    • \w – full current directory
    • \W – basename of current directory
    • \t – time (HH:MM:SS)
    • \d – date (weekday month day)
    • \n – new line
    • \$ – shows $ for normal user, # for root
    • \e[ – start color codes (see tips)
  6. Add colors (optional). To make parts of the prompt colored, use ANSI escape codes. For example, to make the directory green:
    PS1='\[\e[32m\]\w\[\e[0m\]\$ '
    The \[ and \] tell bash that the codes are non-printing (so line wrapping works). Common colors: 30=black, 31=red, 32=green, 33=yellow, 34=blue, 35=magenta, 36=cyan, 37=white.
  7. Add git branch (optional). If you use git, you can show the current branch. Add this function to your .bashrc (above the PS1 line):
    function git_branch() {
      branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
      if [ -n "$branch" ]; then
        echo " ($branch)"
      fi
    }
    Then include it in PS1: PS1='\u@\h:\w\[\e[32m\]$(git_branch)\[\e[0m\]\$ '
  8. Save and apply. In nano, press Ctrl+O, then Enter, then Ctrl+X to exit. Then run source ~/.bashrc to apply the change immediately (or just open a new terminal).
  9. Test your prompt. You should see your new prompt. Try changing directories with cd /tmp to see the directory update.
  10. Make it permanent. The change is already permanent because it’s in .bashrc. It will load every time you start bash.

Common Mistakes

  • Forgetting to escape special characters. In the PS1 string, you must use \ before codes like \u, not single backslashes. If you use \u in a double-quoted string, bash may interpret it differently.
  • Using single quotes vs double quotes. Use single quotes around PS1 to prevent bash from expanding variables immediately. Double quotes can cause issues if you have $ in the prompt.
  • Not wrapping color codes with \[ and \]. If you omit these, the terminal may not wrap lines correctly, causing text to overlap.
  • Editing the wrong file. Some systems use ~/.bash_profile or ~/.profile. If your prompt doesn’t change, check if you’re using bash or another shell (like zsh).

Tips

  • Keep it short. A prompt with too much info can clutter the screen. Pick only what you use daily.
  • Use a newline for long prompts. Add \n at the end of your PS1 to put the cursor on a new line, giving you more room to type.
  • Test in a subshell. Before editing .bashrc, test your PS1 by typing PS1='your_test' directly in the terminal. If you mess up, you can reset with PS1='\u@\h:\w\$ '.
  • Backup your .bashrc. Copy it to .bashrc.bak before making changes, so you can restore if needed.
  • Use a prompt generator. Online tools like bashrcgenerator.com can help you build a prompt visually, but always verify the output.

Troubleshooting

Prompt doesn’t change after source. Check that you edited the correct file and that there’s no other PS1 assignment later in the file overriding yours. Also ensure you’re in bash (echo $0 should show -bash).

Colors show as raw codes like \[\e[32m\]. This means your PS1 is using double quotes or the escapes are wrong. Use single quotes and make sure you have \[ and \] around color codes.

Prompt is broken after a mistake. Run export PS1='\u@\h:\w\$ ' to reset temporarily, then fix your .bashrc.

Git branch not showing. Make sure the function is defined before PS1 uses it, and that you’re in a git repository. Also check that git is installed.

Line wrapping is messed up. This is almost always due to missing \[ and \] around non-printing characters. Add them.

If nothing works, restart your terminal. Sometimes the terminal emulator caches the prompt. Close and reopen it.

Frequently Asked Questions

Can I change the prompt for all users?

Yes, edit /etc/bash.bashrc or /etc/profile (depending on your distribution), but this requires root privileges and affects all users.

How do I add the exit code of the last command?

Add \$? to your PS1. It shows the exit status of the previous command (0 means success). For example: PS1='[\u@\h \w] \$?\$ '

Why does my prompt show 'bash: PROMPT_COMMAND: command not found'?

This error usually means you have a typo in your .bashrc, often in a function or command substitution. Check the line that sets PROMPT_COMMAND (if any) and correct any syntax errors.

Can I use emojis in my prompt?

Yes, if your terminal supports Unicode. Just paste the emoji directly into the PS1 string, but be aware it may affect line wrapping. Test it.

How do I make the prompt different for root?

You can use an if statement in .bashrc: if [ "$EUID" -eq 0 ]; then PS1='\u@\h:\w# '; else PS1='\u@\h:\w\$ '; fi

Does this work in other shells like zsh or fish?

No, this guide is specific to bash. Zsh uses PROMPT with different syntax, and fish has its own function. Search for those separately.