Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Customizing the Shell Prompt

Introduction

The shell prompt is the text or symbols displayed in the command line interface (CLI) that indicates the shell is ready to receive commands. Customizing your shell prompt can make your terminal more informative and visually appealing. In this tutorial, we will cover how to customize the shell prompt in different shells, primarily focusing on bash and zsh.

Understanding the Default Shell Prompt

The default prompt in bash is usually represented by \u@\h:\w\$. Let's break down what each component means:

  • \u - Username
  • \h - Hostname
  • \w - Current working directory
  • \$ - Indicates whether the user is root (#) or not ($)

Example of a default prompt:

user@hostname:~$

Customizing the Bash Prompt

To customize the bash prompt, you need to modify the PS1 variable. This can be done temporarily or permanently.

Temporarily Changing the Bash Prompt

To change the prompt for the current session, you can use the following command:

PS1="<desired prompt>"

Permanently Changing the Bash Prompt

To make the change permanent, you need to add the PS1 variable to your .bashrc file:

Open the .bashrc file:

nano ~/.bashrc

Add the following line:

PS1="<desired prompt>"

Save the file and reload it:

source ~/.bashrc

Example of a Custom Bash Prompt

Here's an example of a customized PS1 variable that includes the username, hostname, and the current working directory:

PS1="\u@\h:\w\$ "
user@hostname:~/projects$

Customizing the Zsh Prompt

Customizing the zsh prompt is similar to bash, but it uses the PROMPT variable instead of PS1.

Temporarily Changing the Zsh Prompt

To change the prompt for the current session, you can use the following command:

PROMPT="<desired prompt>"

Permanently Changing the Zsh Prompt

To make the change permanent, you need to add the PROMPT variable to your .zshrc file:

Open the .zshrc file:

nano ~/.zshrc

Add the following line:

PROMPT="<desired prompt>"

Save the file and reload it:

source ~/.zshrc

Example of a Custom Zsh Prompt

Here's an example of a customized PROMPT variable that includes the username, hostname, and the current working directory:

PROMPT="%n@%m:%~$ "
user@hostname:~/projects$

Adding Colors to the Prompt

Adding colors to your shell prompt can make it more readable and visually appealing. Both bash and zsh use ANSI escape sequences to add color.

Example: Colored Bash Prompt

Here's an example of a PS1 variable with colors:

PS1="\[\e[32m\]\u@\h:\[\e[34m\]\w\[\e[0m\]\$ "
user@hostname:
~/projects
$

Example: Colored Zsh Prompt

Here's an example of a PROMPT variable with colors:

PROMPT="%F{green}%n@%m:%F{blue}%~%f$ "
user@hostname:
~/projects
$

Conclusion

Customizing your shell prompt can greatly enhance your command-line experience by providing more information at a glance and making your terminal more visually appealing. Whether you're using bash or zsh, you now have the knowledge to customize your prompt to suit your needs.