directory

Environment variable PS1

color

Environment variables PS2, PS3, PS4


When the user enters Bash, Bash displays a command prompt that prompts the user to enter commands following that location.

Environment variable PS1

The command prompt is usually the dollar sign $or the pound sign # for the root user. This symbol is determined by the environment variable PS1. Execute the following command to see the definition of the current command prompt.

$ echo $PS1
Copy the code

Bash allows you to customize the command prompt by overwriting this variable. The rewritten PS1 can be stored in the Bash configuration file.bashrc of the user, and the new prompt will take effect when you create a Bash dialogue later. To see the modified prompt in the current window, execute the following command.

$ source ~/.bashrc
Copy the code

The definition of a command prompt that can contain special escape characters to represent specific content.

  • \aThe computer makes a sound when the bell rings.
  • \d: Indicates the current date in the format of week, month, or day, for example, Mon May 26.
  • \h: Indicates the host name of the local host.
  • \H: Indicates the full host name.
  • \j: The number of jobs running in the current Shell session.
  • \l: Indicates the name of the current terminal.
  • \n: a newline character.
  • \r: a carriage return character.
  • \s: The name of the Shell.
  • \tB: It’s a 24-hour systemhours:minutes:secondsFormat Indicates the current time.
  • \T: Current time in the 12-hour system.
  • \ @B: On a 12-hour scheduleAM/PMFormat Indicates the current time.
  • \AB: It’s a 24-hour systemhours:minutesIndicates the current time.
  • \u: Indicates the current user name.
  • \v: Shell version number.
  • \V: Shell version number and release number.
  • \w: Indicates the current working path.
  • \W: Current directory name.
  • The \!: Indicates the number of the current command in the command history.
  • \ #: Indicates the number of commands in the current shell session.
  • \ $: Is displayed for common users$The root user is displayed as#Characters.
  • \ [: Indicates the start of a sequence of non-printed characters.
  • \]: Indicates the end of a sequence of non-printed characters.

For example, the prompt definition [\u@\h \W]\$is displayed as [user@host ~]$(depending on your system).

[user@host ~]$ echo $PS1
[\u@\h \W]\$
Copy the code

This command prompt can be changed by rewriting the PS1 variable.

$ PS1="\A \h \$ "
17:33 host $
Copy the code

Note that the $is best followed by a space, so that the user’s input and the prompt are not conjoined.

 

color

By default, the command prompt displays the predefined colors of the terminal. Bash allows you to customize prompt colors.

Using the following code, you can set the color of the text that follows.

  • \033[0;30m: black
  • \033[1;30m: dark grey
  • \033[0;31mRed:
  • \033[1;31mLighter red:
  • \033[0;32mGreen:
  • \033[1;32m: light green
  • \033[0;33m: brown
  • \033[1;33mYellow:
  • \033[0;34mBlue:
  • \033[1;34m: the light blue
  • \033[0;35mPink:
  • \033[1;35m: pale pink
  • \033[0;36m: cyan
  • \033[1;36mShallow blue:
  • \033[0;37m: light grey
  • \033[1;37mWhite:

For example, if you wanted to set the prompt to red, you could set PS1 to the following code.

PS1='\[\033[0;31m\]<\u@\h \W>\$'
Copy the code

However, after this setting, the text the user enters after the prompt is also red. To solve this problem, you can add another special code \[\033[00m\] at the end to restore the subsequent text to the default color.

PS1='\[\033[0;31m\]<\u@\h \W>\$\[\033[00m\]'
Copy the code

In addition to setting the foreground color, Bash also allows you to set the background color.

  • \033[0;40mBlue:
  • \033[1;44m: black
  • \033[0;41mRed:
  • \033[1;45mPink:
  • \033[0;42mGreen:
  • \033[1;46m: cyan
  • \033[0;43m: brown
  • \033[1;47m: light grey

Below is a prompt with a red background.

PS1='\[\033[0;41m\]<\u@\h \W>\$\[\033[0m\] '
Copy the code

 

Environment variables PS2, PS3, PS4

In addition to PS1, Bash provides three other prompt related environment variables.

The environment variable PS2 is the system prompt for command-line line-breaking input, which defaults to >.

$ echo "hello
> world"
Copy the code

In the preceding command, enter Hello and press Enter. The system prompts you to continue typing. In this case, the prompt displayed in the second line is > as defined by PS2.

The environment variable PS3 is the prompt for the system to enter the menu when using the select command.

The environment variable PS4 defaults to +. It is the prompt that appears at the beginning of each line of command printed before execution when a script is executed using Bash’s -x argument.

For example, here is the script test.sh.

#! /bin/bash

echo "hello world"
Copy the code

 

Execute the script with the -x argument.

$ bash -x test.sh
+ echo 'hello world'
hello world
Copy the code

In the example above, the first line of output is preceded by a +, which is defined by the variable PS4.

 

That’s it for this Bash script tutorial! Thank you!