menu

How to Customize Bash Colors and Content

How to Customize Bash Colors and Content in Linux Terminal Prompt

» How to Customize Bash Colors and Content in Linux Terminal Prompt

The PS1 Bash Environment Variable

The command prompt and terminal appearance are governed by an environment variable called PS1. According to the Bash man page, PS1 represents the primary prompt string which is displayed when the shell is ready to read a command.

The allowed content in PS1 consists of several backslash-escaped special characters whose meaning is listed in the PROMPTING section of the man page.

To illustrate, let’s display the current content of PS1 in our system (this may be somewhat different in your case):

$ echo $PS1

[\u@\h \W]$

Customizing the PS1 Format

According to the PROMPTING section in the man page, this is the meaning of each special character:

  • \u: the username of the current user.
  • \h: the hostname up to the first dot (.) in the Fully-Qualified Domain Name.
  • \W: the basename of the current working directory, with $HOME abbreviated with a tilde (~).
  • $: If the current user is root, display #, $ otherwise.

For example, we may want to consider adding \! If we want to display the history number of the current command, or \H if we want to display the FQDN instead of the short server name.

In the following example we will import both into our current environment by executing this command:

PS1="[\u@\H \W \!]$"

Reference

For more information, see the reference link.

» How to Customize Bash Colors and Content in Linux Terminal Prompt



KF

Comments