Command prompt

After the user enters Bash, Bash will display a command prompt to prompt the user to enter a command behind the location.

Environment Variable PS1

The command prompt is usually the dollar sign $, for root users it is the pound sign #. This symbol is determined by the environment variable PS1, execute the following command, you can see the definition of the current command prompt.

$ echo $PS1

Bash allows users to customize the command prompt by just overwriting this variable. The rewritten PS1 can be placed in the user's Bash configuration file .bashrc, and the new prompt will take effect when a new Bash dialog is created in the future. To see the modified prompt in the current window, you can execute the following command.

$ source ~/.bashrc

The definition of the command prompt can include special escape characters to indicate specific content.

  • \a: The bell rings, and the computer makes a sound.
  • \d: Express the current date in the format of week, month, and day, such as "Mon May 26".
  • \h: The host name of this machine.
  • \H: The full host name.
  • \j: The number of jobs running in the current Shell session.
  • \l: The name of the current terminal device.
  • \n: A newline character.
  • \r: a carriage return character.
  • \s: The name of the shell.
  • \t: The 24-hour format hours:minutes:seconds indicates the current time.
  • \T: The current time in 12-hour format.
  • \@: The 12-hour AM/PM format represents the current time.
  • \A: hours:minutes in the 24-hour format represents the current time.
  • \u: Current user name.
  • \v: Shell version number.
  • \V: Shell version number and release number.
  • \w: ​​The current working path.
  • \W: The name of the current directory.
  • \!: The number of the current command in the command history.
  • \#: The number of commands in the current shell session.
  • \$: Normal users are displayed as $ characters, and root users are displayed as # characters.
  • \[: The start mark of a sequence of non-printing characters.
  • \]: The end mark of a sequence of non-printing characters.

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

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

You can change this command prompt by rewriting the PS1 variable.

$ PS1="\A \h \$ "
17:33 host $

Note that it is best to have a space after $, in this case, the user's input and the prompt will not be connected together.

colour

By default, the command prompt displays the predefined colors of the terminal. Bash allows custom prompt colors.

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

  • \033[0;30m: black
  • \033[1;30m: dark gray
  • \033[0;31m: Red
  • \033[1;31m: light red
  • \033[0;32m: green
  • \033[1;32m: light green
  • \033[0;33m: Brown
  • \033[1;33m: yellow
  • \033[0;34m: blue
  • \033[1;34m: light blue
  • \033[0;35m: pink
  • \033[1;35m: light pink
  • \033[0;36m: Cyan
  • \033[1;36m: light cyan
  • \033[0;37m: light gray
  • \033[1;37m: white

For example, if you want to set the prompt to red, you can set PS1 to the following code.

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

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

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

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

  • \033[0;40m: blue
  • \033[1;44m: black
  • \033[0;41m: Red
  • \033[1;45m: pink
  • \033[0;42m: green
  • \033[1;46m: Cyan
  • \033[0;43m: Brown
  • \033[1;47m: light gray

Below is a prompt with a red background.

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

Environmental variables PS2, PS3, PS4

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

The environment variable PS2 is the prompt of the system when the command line is line-folded and input, and the default is > .

$ echo "hello
> world"

In the above command, after typing hello and pressing the Enter key, the system will prompt to continue typing. At this time, the prompt displayed on the second line is > defined by PS2.

The environment variable PS3 is the prompt of the system input menu when the select command is used.

The environment variable PS4 defaults to + . It is the prompt that appears at the beginning of each line of commands when the script is executed using the -x parameter of Bash.

For example, the following is the script test.sh.

#!/bin/bash

echo "hello world"

Use the -x parameter to execute this script.

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

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