典型的命令提示符示例

在Ubuntu 14.04 LTS Linux发行版中

john@ubuntu01-pc:~$
root@ubuntu01-pc:~#

请注意, $符号表示正在使用的帐户类型。(#表示root用户和$表示普通用户)

openSUSE 13.2 Linux发行版

tux@linux-road:~>
linux-road:~ #

请注意,只有根帐户使用 符号。

更改PS1提示的示例

当前设置:

tux@linux-z2jb:~> echo $PS1
\u@\h:\w>

主PS1提示符的当前设置为\u@\h:\w>从他的上面的输出中,我们可以看到 \u指定了当前用户, \h指定了主机名 , \w指定当前的工作目录,$HOME缩写为波浪号 ~用作简单的分隔符。

如果要更改提示符以显示用户,完整主机名,当前工作目录,然后显示时间,则可以指定类似于以下示例的字符串:(
只需在您当前的命令提示符下键入PS1='[\u@\H:\w:\A]:\$ '

PS1='[\u@\H:\w:\A]:$ '

[tux@linux-z2jb.site:/home:21:22]:$

现在,我们有了一个自定义PS1提示符。作为一个简单的示例,我创建了一个交互式循环,该循环显示PS2变量的值 >输入命令的第一行:
for $ PS1 $ PS2中的i,然后按Enter,将显示PS2提示。因为它需要更多信息才能完成命令序列:

[tux@linux-z2jb.site:/home:21:23]:$ for i in $PS1 $PS2
> do
> echo $i
> done
[\u@\H:\w:\A]:$
>

如果我们想将PS2提示值从 >更改为 =>,我们可以执行命令:PS2 ='=>'

[tux@linux-z2jb.site:/home:21:24]:$ PS2='=> '

如果再次从命令行运行交互式循环,则可以看到新的PS2值:

[tux@linux-z2jb.site:/home:21:25]:$ for i in $PS1 $PS2
=> do
=> echo $i
=> done
[\u@\H:\w:\A]:$
=>

如果要永久更改shell程序提示符,可以通过在文件底部添加以下几行来修改shell程序启动文件:~/.bashrc

# 我自定义的 PS1  PS2 提示符
PS1='[\u@\H:\w:\A]:$ '
PS2='=> '

现在,当您启动shell程序时,将出现新的PS1和PS2提示符!

创建自定义PS1和PS2命令提示符

默认情况下,大多数Shell提示符都会显示与用户和主机名有关的信息。例如:

tux@linux-road:~> whoami
tux

从上面我们可以看到用户 tux已登录到服务器 linux-road

您可能经常看到带有美元符号 $的命令提示符,通常这表示非root用户。您还将看到提示 #,其中的井号表示您正在使用root帐户。

Shell变量 命令提示符

提示符显示的信息由各种shell变量控制:PS1,PS2,PS3和PS4。

要显示您的提示的当前设置,我们可以使用 echo命令以及相关变量:

tux@linux-road:~> echo $PS1
\u@\h:\w>

当以交互方式执行时,Bash在准备读取命令时显示主提示 PS1,而在需要更多输入以完成命令时显示次要提示PS2。

我们可以查看man的说明

       PS1    The value of this parameter is expanded and used as the primary prompt string.  
       PS2    The value of this parameter is expanded as with PS1 and used  as
              the secondary prompt string.  The default is ``> ''.
       PS3    The value of this parameter is used as the prompt for the select
              command 
       PS4    The value of this parameter is expanded  as  with  PS1  and  the
              value  is  printed  before  each command bash displays during an
              execution trace.  The first character of PS4 is replicated  mul-
              tiple  times, as necessary, to indicate multiple levels of indi-
              rection.  The default is ``+ ''.

PS1 将展开此参数的值,并将其用作 主提示字符串。
PS2 该参数的值将与PS1一样展开,并用作辅助提示字符串。默认是>。所以我们在定义函数时,如果遇到换行,就会提示>这个符号。
PS3 将此参数的值用作select命令的提示符
PS4 与PS1一样展开此参数的值,并在执行跟踪期间每个命令bash显示之前打印该值。必要时,PS4的第一个字符被复制多次,以指示多个间接级别。默认是'' + ''。

Bash允许通过插入许多 反斜杠转义特殊字符来自定义这些提示字符串,这些特殊字符的解码方式如下:

              \a     an ASCII bell character (07)
              \d     the date in "Weekday Month Date" format (e.g.,  "Tue  May
                     26")
              \D{format}
                     the  format  is  passed  to strftime(3) and the result is
                     inserted into the prompt string; an empty format  results
                     in a locale-specific time representation.  The braces are
                     required
              \e     an ASCII escape character (033)
              \h     the hostname up to the first `.'
              \H     the hostname
              \j     the number of jobs currently managed by the shell
              \l     the basename of the shell's terminal device name
              \n     newline
              \r     carriage return
              \s     the name of the shell, the basename of  ##代码##  (the  portion
                     following the final slash)
              \t     the current time in 24-hour HH:MM:SS format
              \T     the current time in 12-hour HH:MM:SS format
              \@     the current time in 12-hour am/pm format
              \A     the current time in 24-hour HH:MM format
              \u     the username of the current user
              \v     the version of bash (e.g., 2.00)
              \V     the release of bash, version + patch level (e.g., 2.00.0)
	      \w     the  current  working  directory,  with $HOME abbreviated
                     with a tilde (uses the value of the PROMPT_DIRTRIM  vari-
                     able)
              \W     the basename of the current working directory, with $HOME
                     abbreviated with a tilde
              \!     the history number of this command
              \#     the command number of this command
              $     if the effective UID is 0, a #, otherwise a $
              \nnn   the character corresponding to the octal number nnn
              \     a backslash
              \[     begin a sequence of non-printing characters, which  could
                     be  used  to  embed  a terminal control sequence into the
                     prompt
              \]     end a sequence of non-printing characters
如何创建自定义BASH SHELL提示符
日期:2019-04-29 03:17:26 来源:oir作者:oir