To customize the terminal prompt in Linux, you can modify the PS1 environment variable, which defines the appearance of the primary prompt. Here’s how to do it:
-
Open your terminal.
-
Edit the
.bashrcfile (for Bash shell) or.zshrcfile (for Zsh shell) in your home directory. You can use a text editor likenanoorvim:nano ~/.bashrc -
Add or modify the
PS1variable. For example:export PS1="\u@\h:\w\$ "This will set the prompt to show the username (
\u), hostname (\h), and current working directory (\w). -
Save the file and exit the editor.
-
Apply the changes by running:
source ~/.bashrc
Common Escape Sequences for PS1:
\u: Username\h: Hostname (up to the first dot)\w: Current working directory\d: Current date\t: Current time (24-hour format)\$: Displays#for root and$for regular users
You can customize the prompt further by adding colors and other formatting options. For example, to add color, you can use ANSI escape codes. Here's an example with color:
export PS1="\[\e[32m\]\u@\h:\[\e[34m\]\w\[\e[0m\]\$ "
This will set the username and hostname in green and the working directory in blue.
