Shell prompt (the difference between $ and #)

Start a terminal emulation package or from the Linux console login, you can see the Shell prompt. Shell is the gateway to the prompt, enter the local Shell command.

For ordinary users, Base shell prompt, the default is the dollar sign $; for the superuser (root user), Bash Shell default prompt is the pound sign #. This symbol indicates that Shell is waiting for input commands.

Prompt different formats of different Linux distributions use. CentOS example, the default prompt format:

[mozhiyan@localhost ~]$

This format contains information on the following three aspects:

  • Start Shell user name, that mozhiyan;
  • Local host name, that is localhost;
  • Current directory tilde ~is shorthand notation home directory.


By Shell PS1and PS2controlled environment prompt format two variables:

  • PS1 control command line prompt outermost format.
  • PS2 format command line prompt control of the second layer.


Shell first input command in using the prompt format designated PS1; If you enter a command to enter additional information required, PS2 using Shell will prompt the specified format. Consider the following example:

  1. [mozhiyan @ localhost ~] $ echo "C language"
  2. C language
  3. [mozhiyan@localhost ~]$ echo "http://c.biancheng.net"
  4. http://c.biancheng.net
  5. [mozhiyan@localhost ~]$ echo "
  6. > yan
  7. > chang
  8. > sheng
  9. > "
  10. yan
  11. chang
  12. sheng
  13. [mozhiyan@localhost ~]$

echo command is output, a digital output can be used, variables, string and the like; in the present embodiment, we use the echo to the output string.

A string is a group consisting of " "a sequence of characters surrounded up, the first echo "as the beginning of the string, the second "as the end of the string. Here it can be seen as a string of additional information echo command.

In the present embodiment, when the first two are used in echo command followed by a string of one line to enter the full additional information. The third time using echo, the string into multiple lines, echo encountered first "consider the additional information is not complete, it will continue to wait for user input, until she met the second ". Additional information input command is the second layer, it is used >as a prompt.

To display the current format prompt, you can use echo output PS1 and PS2:

  1. [mozhiyan@localhost ~]$ echo $PS1
  2. [\u@\h \W]\$
  3. [mozhiyan@localhost ~]$ echo $PS2
  4. >
  5. [mozhiyan@localhost ~]$

Shell used \to precede the special character to represent the elements contained in the command prompt, which makes the PS1 and PS2 formats may seem a bit strange. The following table shows the special characters you can use in PS1 and PS2 in.

Bash shell prompt can contain elements of
character description
\a Bell character
\d The format is "month year" date
\e ASCII escape character
\h Local host name
\H Fully qualified domain qualified host name
\j shell current job number management
\1 The basic name of the shell's terminal device name
\n ASCII newline characters
\r ASCII carriage return
\s Name of the shell
\t Format "hours: minutes: seconds," the 24-hour current time
\T Format "hours: minutes: seconds," the 12-hour current time
\@ Current time format am / pm to 12 hour
\ in The current user's username
\ v bash shell version
\ V bash shell's release level
\w The current working directory
\W The basic name of the current working directory
\! The bash shell command history number
\# The number of commands the command
\$ If you are a regular user, compared with the dollar sign $; if the superuser (root user), compared with the pound sign #.
\nnn The corresponding character in the octal value nnn
\\ Slash
\[ The control code sequence beginning
\] End of the sequence control code


Note that all the special characters are the backslash \at the beginning, the purpose is to distinguish ordinary characters. You can use any combination of the above special characters in the command prompt.

We can modify the format by modifying the PS1 prompt variable, for example:

  1. [mozhiyan@localhost ~]$ PS1="[\t][\u]\$ "
  2. [ 17 : 27 : 34 ] [Moshhian ] $

The new Shell prompt can now display the current time and user name. But this new definition of PS1 variable is only valid during the current session Shell, will re-use the default format when starting Shell prompt again.

Guess you like

Origin www.cnblogs.com/chengkanghua/p/11098961.html