Ultimate Guide to Linux PS1 Prompt Customization
PS1, or Prompt String 1, is the primary prompt variable that defines the appearance and elements of the command line prompt within a shell environment in Linux.
It offers multiple customization options to make your command prompt more informative and visually appealing. This tutorial will discuss these details, enhancing your command-line interface experience.
- 1 Viewing current PS1
- 2 PS1 escape sequences
- 3 Simple Text Customization
- 4 Using Colors in PS1
- 5 Conditional Customization
- 6 Displaying Git Branch Information
- 7 Utilizing Subshell Functions
- 8 Using ASCII Art and Custom Symbols
- 9 Integrating Time and Date
- 10 Handling Multiple Lines in PS1
- 11 Controlling Cursor Movement
- 12 Dynamically Modifying PS1
- 13 Loading Custom PS1 based on Context
Viewing current PS1
You can view the current PS1 string with the following command:
echo $PS1
Output:
\u@\h:\w\$
The command echo $PS1
displays the value of the PS1 environment variable, representing the current state of your prompt string.
Here, the escape codes function as placeholders for various pieces of information:
\u
: Current username\h
: Hostname up to the first dot\w
: Current working directory\$
: Displays$
for regular users,#
for root
PS1 escape sequences
Here’s a comprehensive list of escape sequences that can be used within PS1 to customize the prompt:
\a
: An ASCII bell character\d
: Date, in “Weekday Month Date” format\e
: An ASCII escape character\h
: Hostname, up to the first dot\H
: Full hostname\j
: Number of jobs currently managed by the shell\l
: The basename of the shell’s terminal device name\n
: Newline\r
: Carriage return\s
: The name of the shell\t
: Current time in 24-hour HH:MM:SS format\T
: Current time in 12-hour HH:MM:SS format\@
: Current time in 12-hour a.m./p.m. format\u
: Current username\v
: Version of Bash\V
: Release of Bash, version + patch level\w
: Current working directory, with $HOME abbreviated with a tilde\W
: Basename of$PWD
, with$HOME
abbreviated with a tilde\!
: History number of this command\#
: Command number of this command\$
: If UID is 0,#
, otherwise$
\\
: A backslash\nnn
: The character corresponding to the octal number nnn\e[...m
: A terminal escape sequence (allows colorization)
You can use these escape sequences to craft a highly informative and pleasing command prompt.
Simple Text Customization
You can simply include any text you desire in the PS1 string. Here’s an example:
export PS1="MyCustomPrompt:\w\$ "
Output:
MyCustomPrompt:/path/to/directory$
Here, the text “MyCustomPrompt” has been placed at the beginning of the prompt string, followed by the current working directory.
Using Colors in PS1
Here’s how to use colors within PS1:
export PS1="\e[31m\u@\h:\e[32m\w\e[0m\$ "
Output:
user@hostname:/path/to/directory$
Here, the username and hostname are in red (\e[31m
), and the working directory is in green (\e[32m
).
The \e[0m
resets the color back to the default for subsequent text.
\e[31m
: Red\e[32m
: Green\e[33m
: Yellow\e[34m
: Blue\e[35m
: Magenta\e[36m
: Cyan\e[37m
: White
You can use these color codes to create visually appealing and informative prompts.
Conditional Customization
You can customize the prompt based on condition. Here’s how to do it:
if [ "$USER" = "root" ]; then PS1="\e[31m\u@\h:\w\$\e[0m " else PS1="\u@\h:\w\$ " fi
Command Output for root user:
root@hostname:/path/to/directory$
This snippet checks if the current user is “root” and applies a red color to the prompt for root, leaving it uncolored for other users.
Such conditional customizations can be extended to include other properties like hostname, environment, etc.
Displaying Git Branch Information
For those working with Git repositories, it can be handy to include the current branch name in the prompt. Here’s how:
export PS1="\u@\h:\w\$(git rev-parse --abbrev-ref HEAD 2>/dev/null)\$ "
Output:
user@hostname:/path/to/repo/master$
The command git rev-parse --abbrev-ref HEAD
retrieves the current Git branch name, and it’s executed within the prompt string itself.
If not in a Git repository, it won’t display anything after the working directory.
Utilizing Subshell Functions
You can use subshell functions to incorporate dynamic content into the PS1. Here’s an example:
get_loadavg() { awk '{ print $1 }' /proc/loadavg } export PS1="\u@\h:\w Load:\$(get_loadavg)\$ "
Output:
user@hostname:/path/to/directory Load:0.45$
Here, the get_loadavg
function fetches the system’s load average from /proc/loadavg
, and the prompt executes this function, displaying the load average.
Using ASCII Art and Custom Symbols
You can include ASCII art or custom symbols in your prompt. Here’s a simple example:
export PS1="🚀 \u@\h:\w\$ "
Output:
🚀 user@hostname:/path/to/directory$
Here, a rocket emoji is placed at the start of the prompt, followed by the usual elements.
You can use other symbols, emojis, or even ASCII art to create visually engaging prompts.
Integrating Time and Date
You can include time and date in your prompt. Here’s how to display the current time:
export PS1="\t \u@\h:\w\$ "
Output:
14:35:22 user@hostname:/path/to/directory$
By using the \t
escape sequence, the current time in 24-hour format is displayed at the beginning of the prompt.
You can also use \d
to display the date, in “Weekday Month Date” format.
Handling Multiple Lines in PS1
Having multiple lines in your prompt can help organize information. Here’s how to create a two-line prompt:
export PS1="\u@\h:\w\n\$ "
Output:
user@hostname:/path/to/directory $
By incorporating \n
into the PS1 string, you can create a newline, thus breaking the prompt into two lines. This can be extended to more lines as needed.
Controlling Cursor Movement
Controlling cursor movement in PS1 allows for complex layouts and effects. Here’s an example of aligning the cursor to the right side:
export PS1="\e[s\e[40C|\e[u\u@\h:\w\$ "
Output:
|user@hostname:/path/to/directory$
In this example:
\e[s
: Saves the current cursor position.\e[40C
: Moves the cursor 40 characters to the right.|
: Prints a vertical bar.\e[u
: Restores the cursor to the saved position.
This results in a prompt with the vertical bar aligned to the right, with the usual prompt elements to its left.
Dynamically Modifying PS1
You can dynamically modify PS1 to adapt to different scenarios or conditions. Here’s a basic example to switch between two profiles:
set_profile() { case $1 in projectA) PS1="ProjectA:\w\$ " ;; projectB) PS1="ProjectB:\w\$ " ;; *) PS1="\u@\h:\w\$ " ;; esac }
Output:
ProjectA:/path/to/directory$
By calling the set_profile
function with an argument (e.g., set_profile projectA
), you can switch between predefined prompt styles.
Such dynamic modifications can be extended to create highly adaptable and context-sensitive prompt configurations.
Loading Custom PS1 based on Context
Differentiating prompts based on context, such as SSH sessions or being the root user, can improve awareness. Here’s how to do it:
if [ -n "$SSH_CLIENT" ]; then PS1="\e[34mSSH:\u@\h:\w\$ \e[0m" elif [ "$USER" = "root" ]; then PS1="\e[31m\u@\h:\w\$\e[0m " else PS1="\u@\h:\w\$ " fi
Command Output for SSH session:
SSH:user@hostname:/path/to/directory$
Blue for SSH sessions, red for the root user, and standard color for other contexts.
This conditional block colors the prompt based on whether you are in an SSH session or logged in as the root user.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.