在哪里可以找到 $PS1 变量的完整参考?

Mar*_*ond 24 bash ps1

我的机器(Kubuntu 13.10)上的默认 PS1 变量是这样的:

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
Run Code Online (Sandbox Code Playgroud)

我正在寻找有关 $PS1 变量如何工作的参考,至少可以让我了解上述 PS1 变量。

Mar*_*ond 27

参考

到目前为止,还没有关于 Bash 提示中所有内容的单一参考 - 但由于它是一个已经发展了几十年的功能,并且可能因发行版而异,所以可能要求太多了。我试图总结我在这里发现最有用的内容。

此操作方法是最完整的,但非常冗长且杂乱无章。一些更有用的部分:

  • 2.42.5解释了设置 PS1 的基础知识,包括(可打印的)转义字符。
  • 第 3.4 节解释了原因\[\]必要性。
  • 第 6 节解释了您可能想要使用的所有主要(不可打印)转义序列,包括设置提示的颜色和 xterm 窗口的标题。

本指南解释了${}在 Bash 中的工作原理,这个 Ask Ubuntu 问题解释了更多关于它如何与debian_chroot.

在这些之间,我认为默认 Ubuntu PS1 变量中的每个字符都得到了解释。

Ubuntu 提示说明

提示分为三部分:

  • \[\e]0;\u@\h: \w\a\] 设置 xterm 窗口的标题栏:

    • \[ 开始一段不可打印的字符
    • \e]0; 是 'set xterm title' 的转义序列(我相信 0 以外的数字会设置其他 xterm 属性,尽管我还没有测试过)
    • \u@\h: \w要使用的标题(请参阅下面的\u,\h\w
    • \a 标志着标题的结束
    • \] 标记不可打印字符的结束
  • ${debian_chroot:+($debian_chroot)}如果设置了 $debian_chroot,则扩展为括号中的 $debian_chroot 值。有关 $debian_chroot 的更多信息,请参阅此问题

  • \u@\h:\w\$ 是提示本身:

    • \u 扩展到当前用户名
    • \h 扩展到当前主机名
    • \w 扩展到当前工作目录
    • \$扩展到#root 和$所有其他用户


Rad*_*anu 8

根据Bash 参考手册PS1是:

主要提示字符串。默认值为\s-\v\$。有关在显示之前展开的转义序列的完整列表,请参阅控制提示PS1

其他一些很好的参考可以是:


kir*_*iri 6

ss64.com似乎有我找到的最好的参考。

它解释了以下变量:

\d   The date, in "Weekday Month Date" format (e.g., "Tue May 26"). 

\h   The hostname, up to the first . (e.g. deckard) 
\H   The hostname. (e.g. deckard.SS64.com)

\j   The number of jobs currently managed by the shell. 

\l   The basename of the shell's terminal device name. 

\s   The name of the shell, the basename of $0 (the portion following 
    the final slash). 

\t   The time, in 24-hour HH:MM:SS format. 
\T   The time, in 12-hour HH:MM:SS format. 
\@   The time, in 12-hour am/pm format. 

\u   The username of the current user. 

\v   The version of Bash (e.g., 2.00) 

\V   The release of Bash, version + patchlevel (e.g., 2.00.0) 

\w   The current working directory. 
\W   The basename of $PWD. 

\!   The history number of this command. 
\#   The command number of this command. 

\$   If you are not root, inserts a "$"; if you are root, you get a "#"  (root uid = 0) 

\nnn   The character whose ASCII code is the octal value nnn. 

\n   A newline. 
\r   A carriage return. 
\e   An escape character. 
\a   A bell character.
\\   A backslash. 

\[   Begin a sequence of non-printing characters. (like color escape sequences). This
     allows bash to calculate word wrapping correctly.

\]   End a sequence of non-printing characters.
Run Code Online (Sandbox Code Playgroud)

\[ ... \]定义了一系列的非打印字符。他们需要正确跟踪光标位置。

\e你开始提示转义序列。更多关于这里的内容(注意该页面上的“Esc”是\e序列)。

  • 注意:我从来没有真正喜欢过转义序列。使用tput以获得换码为您服务。

${debian_chroot:+($debian_chroot)}是参数扩展。看这里

  • 它写($debian_chroot)如果$debian_chroot设置否则什么都没有。