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

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

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 

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

参考

到目前为止,还没有针对Bash提示中所有内容的单一参考 – 但由于它是一个已经发展了几十年的function,可能因发行版到发行版而有所不同,也许这个问题太多了。 我试图总结一下我发现的最有用的东西。

这个方法是最完整的,但是很长而且漫无边际。 一些更有用的部分:

  • 第2.4节和第2.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, \$扩展为所有其他用户

根据Bash参考手册 , PS1是:

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

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

  • PS1提示解释为Linux / Unix

  • 在我的终端提示中“$ {debian_chroot:+($ debian_chroot)}”做了什么?

  • Bash Shell PS1:10个像Angelina Jolie一样制作Linux提示的例子

ss64.com似乎有我发现的最佳参考。

它解释了以下变量:

 \d The date, in "Weekday Month Date" format (eg, "Tue May 26"). \h The hostname, up to the first . (eg deckard) \H The hostname. (eg 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 (eg, 2.00) \V The release of Bash, version + patchlevel (eg, 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. 

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

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

  • 注意:我从来没有真正喜欢转义序列。 使用tput为您获取转义码。

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

  • 如果没有设置($debian_chroot)它会写入($debian_chroot)