How to customize the prompt?

0117

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:

  1. Open your terminal.

  2. Edit the .bashrc file (for Bash shell) or .zshrc file (for Zsh shell) in your home directory. You can use a text editor like nano or vim:

    nano ~/.bashrc
  3. Add or modify the PS1 variable. For example:

    export PS1="\u@\h:\w\$ "

    This will set the prompt to show the username (\u), hostname (\h), and current working directory (\w).

  4. Save the file and exit the editor.

  5. 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.

0 Comments

no data
Be the first to share your comment!