Modifying a Prompt

Learn how to modify a prompt in a shell environment.

The PS1 variable

PS1 is a primary prompt variable, The value of the PS1 variable lets us specify what our prompt should look like. We can put lots of information in our prompt, including our username, our current working directory, and much more.

Changing the prompt text

Let’s start by creating a prompt that’s nothing more than the dollar sign with space after it. We add this line to our~/.bashrc file:

PS1='\$ '

The \$ sequence displays a dollar sign for regular users and a pound sign (#) for root users. Using single quotes around the prompt value ensures that the value isn’t expanded into a literal dollar sign.

Let’s add the current working directory to the prompt. We add \w, which shows the full path to the current directory unless the directory is in the home directory, in which case it displays ~ instead:

PS1='\w \$ '

To add user@host: to the front of the path, which lets us see the username and hostname, we use \u for username, the literal @, and \h for the hostname, followed by a colon (:):

PS1='\u@\h:\w \$ '

Changing the prompt color

We can use color codes in the prompt as well by using ANSI color codes.

Run the terminal below to try this command:

$ echo -e "\033[36mhello\033[0m world"

Get hands-on with 1400+ tech skills courses.