Modifying a Prompt
Explore how to personalize the command-line prompt by modifying the PS1 variable to display useful information like username and current directory. Understand how to add colors using ANSI codes and tput for improved visual clarity. Learn to make your prompt easier to read and maintain by using variables and sourcing configuration files.
We'll cover the following...
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 ...