What is the $PS1 Variable in Linux — Unix
The $PS1 variable in Linux is an environment variable that defines the format of the command prompt displayed in the terminal.It stands for “Prompt String 1” and is used to customize the appearance of the prompt. By default, the bash prompt includes the current username, hostname, and current working directory, followed by the $ symbol for a regular user or the # symbol for the root user.
The contents of $PS1 are interpreted by the shell and displayed as the command prompt before each new command line. It can contain various escape sequences and special characters that are expanded or replaced with specific values when the prompt is displayed.
By customizing the value of $PS1, you can personalize the appearance of your command prompt to include information such as the current username, hostname, working directory, time, and more. You can also use colors and formatting options to make the prompt more visually appealing or informative.
Here are some common escape sequences that can be used in $PS1:
\u: Inserts the username of the current user.\h: Inserts the hostname of the system.\w: Inserts the current working directory.\W: Inserts the base name of the current working directory.\n: Inserts a newline.\t: Inserts the current time in 24-hour format (HH:MM:SS).\d: Inserts the current date in the format "Weekday Month Day" (e.g., "Mon Sep 20").\$:: Inserts a$symbol for a regular user or#symbol for the root user.
For example, setting $PS1 to "\u@\h:\w$ " would result in a prompt that displays the username, hostname, and current working directory followed by a $ symbol.
You can customize the value of $PS1 in your shell startup files like .bashrc or .bash_profile to have a persistent custom prompt whenever you open a new shell session.
Note: The appearance and behavior of the prompt can vary depending on the shell you are using (e.g., Bash, Zsh). The $PS1 variable specifically applies to the Bash shell.
How to add color to your $PS1 prompt
# Set a colorful value for $PS1
PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "\[\033[01;32m\]: This sequence sets the color of the username and hostname to bright green.\[\033[00m\]: This sequence resets the color to the default for the portion that follows it.\[\033[01;34m\]: This sequence sets the color of the current working directory to bright blue.\$: This represents the regular$symbol for a regular user, or#symbol for the root user.
By using these escape sequences, the prompt will display the username and hostname in bright green, the current working directory in bright blue, and the regular $ or # symbol at the end.
Get Linux Root Room’s stories in your inbox
Join Medium for free to get updates from this writer.
You can customize the colors by modifying the ANSI escape sequences. Here are some commonly used color codes:
- Black:
\[\033[0;30m\] - Red:
\[\033[0;31m\] - Green:
\[\033[0;32m\] - Yellow:
\[\033[0;33m\] - Blue:
\[\033[0;34m\] - Purple:
\[\033[0;35m\] - Cyan:
\[\033[0;36m\] - White:
\[\033[0;37m\]
You can experiment with different colors and formatting options to create your desired prompt appearance. Just make sure to enclose the ANSI escape sequences between \[ and \] to indicate that they are non-printing characters, which helps the shell calculate the correct prompt length.
Remember to set the modified $PS1 value in your shell startup file (e.g., .bashrc or .bash_profile) to have a persistent colorful prompt across shell sessions.
How to change the $PS1 permanently
To change the $PS1 permanently, you can follow these steps:
To change the $PS1 permanently, you can follow these steps:
- Open the
~/.bashrcfile in a text editor. This file is located in your home directory and contains configuration settings for the bash shell. You can use any text editor you prefer, but for this example, we’ll use nano:
nano ~/.bashrc2. Scroll down to the bottom of the file and add the following line:
PS1="\u@\h:\w\$ "This line sets the PS1 environment variable to display the username, hostname, and current working directory.
3. Save the ~/.bashrcfile and exit the text editor.
4. To apply the changes to your current shell session, run the following command:
source ~/.bashrcNow, your bash prompt will be customized according to the value you set for $PS1. This change will persist across all future shell sessions because it was added to the ~/.bashrcfile, which is sourced every time a new shell session starts.
Read More
My YouTube Channel
More shell script videos and linux tutorials on my YouTube Channel.






