linux 自定义bash_如何自定义(和着色)您的Bash提示

linux 自定义bash

linux 自定义bash

Most Linux distributions configure the Bash prompt to look something like username@hostname:directory$ . But you can configure the Bash prompt to contain whatever you like, and even choose whatever colors you like.

大多数Linux发行版都将Bash提示符配置为类似于username@hostname:directory$ 。 但是您可以将Bash提示符配置为包含所需的内容,甚至可以选择所需的颜色。

The example steps here were performed on Ubuntu 16.04 LTS. The process should be the same on other Linux distributions, although the default Bash prompt and settings in the .bashrc file may be a bit different.

此处的示例步骤是在Ubuntu 16.04 LTS上执行的。 在其他Linux发行版上,该过程应相同,尽管.bashrc文件中的默认Bash提示和设置可能有所不同。

提示变量的存储位置 (Where the Prompt Variable is Stored)

Your Bash prompt configuration is stored in your user account’s .bashrc file, which is at ~/.bashrc. So, if your username is bob, the file is at /home/bob/.bashrc.

您的Bash提示配置存储在用户帐户的.bashrc文件中,该文件位于~/.bashrc 。 因此,如果您的用户名是bob,则文件位于/home/bob/.bashrc

You can open the file to view the current Bash variable. We’ll use nano as our example text editor, although you could also use vi, emacs, or any other text editor you’re comfortable with. Open a Terminal and run:

您可以打开文件以查看当前的Bash变量。 我们将使用nano作为示例文本编辑器,尽管您也可以使用vi ,emacs或您喜欢的任何其他文本编辑器。 打开终端并运行:

nano ~/.bashrc

Scroll down to the PS1= section. The first variable looks rather complicated because it includes color information—we’ll explain that later. The second variable, without color information, reads as follows:

向下滚动到PS1=部分。 第一个变量看起来相当复杂,因为它包含颜色信息,我们将在后面解释。 没有颜色信息的第二个变量的内容如下:

${debian_chroot:+($debian_chroot)}\u@\h:\w\$

This is still a little complicated due to the ${debian_chroot:+($debian_chroot)} bits. These just tell Bash to let you know if you’re using a Debian chroot environment and normally won’t be shown. Ignoring those, here’s the default structure of the Bash prompt variable:

由于${debian_chroot:+($debian_chroot)}位,这仍然有些复杂。 这些只是告诉Bash让您知道您使用的是Debian chroot环境,通常不会显示出来。 忽略这些,这是Bash提示变量的默认结构:

\u@\h:\w\$

\u indicates your username, @ indicates the @ sign, \h indicates the hostname (computer name), : indicates the : character, \w indicates the working directory, and \$ indicates a $ if you’re a normal user account or # if you’re root. So, putting that all together, you get username@hostname:working_directory$.

\u表明您的用户名, @表示@符号, \h表示主机名(计算机名), :表示:字符, \w表示工作目录和\$表示$如果你是一个普通用户帐户或#如果您是root。 因此,将它们放在一起,您将得到username@hostname:working_directory$

To change your Bash prompt, you just have to add, remove, or rearrange the special characters in the PS1 variable. But there are many more variables you can use than the default ones.

要更改Bash提示,您只需添加,删除或重新排列PS1变量中的特殊字符。 但是您可以使用的变量比默认变量更多。

Leave the text editor for now—in nano, press Ctrl+X to exit. We’ll show you how to experiment with variables before actually writing a new one into your .bashrc file.

暂时保留文本编辑器-在nano中,按Ctrl + X退出。 我们将向您展示如何在实际将新变量写入.bashrc文件之前尝试使用变量。

如何创建自定义Bash提示 (How to Create a Custom Bash Prompt)

Your Bash prompt configuration is stored in the PS1 variable. To save the contents of the PS1 variable into a new variable, run the following command:

您的Bash提示配置存储在PS1变量中。 要将PS1变量的内容保存到新变量中,请运行以下命令:

DEFAULT=$PS1

You can now set the PS1 variable to different values to experiment. For example, the first line here would set your prompt to a basic “user$” prompt, while the second would set your prompt to a basic “user:working_directory$” prompt.

您现在可以将PS1变量设置为不同的值以进行实验。 例如,此处的第一行会将您的提示设置为基本的“ user $”提示,而第二行会将您的提示设置为基本的“ user:working_directory $”提示。

PS1="\u\$ "

PS1="\u:\w\$ "

If you ever want to get back to your default prompt, just run the following command.

如果您想返回到默认提示,只需运行以下命令。

PS1=$DEFAULT

Bash will be restored to its default prompt thanks to the fact that you saved those default settings earlier. Note that any changes you make here are only temporary for the current Bash session, so you can always sign out and sign back in or close and reopen the terminal window to go back to your default prompt. But the above line makes it possible to easily get back to your default Bash prompt without the hassle of signing out or closing a window.

由于您之前保存了这些默认设置,Bash将恢复为默认提示。 请注意,您在此处所做的任何更改仅是当前Bash会话的临时更改,因此您始终可以退出并重新登录,或者关闭并重新打开终端窗口以返回默认提示。 但是上面的代码行可以轻松返回默认的Bash提示符,而无需退出或关闭窗口。

You can add any characters or text to the variable. So, to prefix the default prompt with “Hello World”, you could use:

您可以将任何字符或文本添加到变量。 因此,要在默认提示前加上“ Hello World”,可以使用:

PS1="Hello World \u@\h:\w\$ "

Now that you’ve got the basics down, you just need to know what all the special characters are. You probably won’t care about many of these, but here’s the full list as it appears in the Bash manual:

现在您已经掌握了基础知识,您只需要知道所有特殊字符是什么。 您可能不会在意其中许多,但是这里是Bash手册中显示的完整列表:

  • A bell character: \a

    响铃字符: \a

  • The date, in “Weekday Month Date” format (e.g., “Tue May 26”): \d

    日期,采用“工作日月份日期”格式(例如,“ 5月26日星期二”): \d

  • 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: \D{format}

    该格式被传递给strftime(3)并将结果插入到提示字符串中。 空格式将导致特定于语言环境的时间表示。 大括号是必需的: \D{format}

  • An escape character: \e

    转义符: \e

  • The hostname, up to the first ‘.’: \h

    主机名,最多第一个“。”: \h

  • The hostname: \H

    主机名: \H

  • The number of jobs currently managed by the shell: \j

    Shell当前管理的作业数: \j

  • The basename of the shell’s terminal device name: \l

    Shell的终端设备名称的基本名称: \l

  • A newline: \n

    换行符: \n

  • A carriage return: \r

    回车符: \r

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

    外壳程序的名称,$ 0的基本名称(最后斜杠后的部分): \s

  • The time, in 24-hour HH:MM:SS format: \t

    时间,以24小时制HH:MM:SS格式: \t

  • The time, in 12-hour HH:MM:SS format: \T

    时间,以12小时HH:MM:SS格式: \T

  • The time, in 12-hour am/pm format: \@

    时间,采用12小时制,例如: \@

  • The time, in 24-hour HH:MM format: \A

    时间,以24小时制HH:MM格式: \A

  • The username of the current user: \u

    当前用户的用户名: \u

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

    Bash的版本(例如2.00): \v

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

    Bash版本+补丁程序级别(例如2.00.0)的发布: \V

  • The current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable): \w

    当前工作目录,$ HOME缩写为波浪号(使用$ PROMPT_DIRTRIM变量): \w

  • The basename of $PWD, with $HOME abbreviated with a tilde: \W

    $ PWD的基本名称,其中$ HOME缩写为波浪号: \W

  • The history number of this command: \!

    此命令的历史记录号: \!

  • The command number of this command: \#

    该命令的命令号: \#

  • If the effective uid is 0, #, otherwise $: \$

    如果有效uid为0,则为#,否则为$: \$

  • The character whose ASCII code is the octal value nnn: \nnn

    ASCII码为八进制值nnn的字符: \nnn

  • A backslash: \\

    反斜杠: \\

  • Begin a sequence of non-printing characters. This could be used to embed a terminal control sequence into the prompt: \[

    开始一系列非打印字符。 这可以用于将终端控制序列嵌入到提示中: \[

  • End a sequence of non-printing characters: \]

    结束一系列非打印字符: \]

So, if you wanted to add the date and time to your Bash prompt and put the working directory on command on a second line, you could use the following construction:

因此,如果要在Bash提示符中添加日期和时间,并将工作目录放在第二行的命令中,则可以使用以下构造:

PS1="[\d \t] \u@\h\n\w\$ "

The square brackets here aren’t necessary at all, but help break things up visually and make the line easier to read. As we covered earlier, you can add any text or normal characters to the variable you like, so feel free to use whatever works for you.

此处的方括号根本不是必需的,但有助于视觉分解内容并使该行更易于阅读。 如前所述,您可以在所需的变量中添加任何文本或普通字符,因此请随意使用适合您的方法。

There’s one more powerful trick you should know about: You can add the output of any command to the prompt. Whenever the prompt appears, Bash will run the command and fill in the current information. To do this, just include any command you want to run between two ` characters. That’s not an apostrophe—that’s the grave accent, which appears above the Tab key on your keyboard.

您应该了解一个更强大的技巧:您可以将任何命令的输出添加到提示中。 每当出现提示时,Bash都会运行命令并填写当前信息。 为此,只需包括要在两个`字符之间运行的任何命令。 这不是撇号,而是重音符号,它出现在键盘上的Tab键上方。

For example, let’s say you want to view the Linux kernel version in the prompt. You could use a line like the following:

例如,假设您要在提示符下查看Linux内核版本。 您可以使用如下所示的行:

PS1="\u@\h on `uname -s -r` \w\$ "

As another example, let’s say you want to view the system’s uptime and load average, as displayed by the uptime command. You could use the following construction, which puts the uptime on its own line before the rest of the prompt.

再举一个例子,假设您要查看系统的正常运行时间和平均负载,如uptime命令所显示。 您可以使用以下构造,将正常运行时间放在提示符其余部分之前的行中。

PS1="(`uptime`)\n\u@\h:\w$ "

Feel free to experiment with different special characters and commands to assemble your ideal command prompt.

随意尝试使用不同的特殊字符和命令来组装理想的命令提示符。

如何为Bash提示添加颜色 (How to Add Colors to Your Bash Prompt)

Once you’ve figured out your preferred prompt, you can add colors to it. This is actually very simple, but it makes the variable look awfully messy and complicated if you don’t understand what you’re looking at.

确定首选提示后,即可为其添加颜色。 这实际上非常简单,但是如果您不了解所要查找的内容,则会使变量看起来非常混乱和复杂。

For example, the default color prompt variable from earlier was:

例如,先前的默认颜色提示变量为:

${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

Or, removing the debian_chroot bits once again:

或者,再次删除debian_chroot位:

\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

This is actually just the \u@\h:\w$ variable from earlier, but with color information. Really, we can break it down into a few sections:

实际上,这只是先前的\u@\h:\w$变量,但是具有颜色信息。 确实,我们可以将其细分为以下几个部分:

\[\033[01;32m\]\u@\h

\[\033[00m\]:

\[\033[01;34m\]\w

\[\033[00m\]\$

The first section is the \u@\h bit, preceded by color information that turns it green. The second is the : character, preceded by color information that removes any coloring. The third is the \w bit, preceded by color information that turns it blue. The fourth is the \$ bit, preceded by color information that removes any coloring.

第一部分是\u@\h位,其后是将其变为绿色的颜色信息。 第二个是:字符,其后是删除所有颜色的颜色信息。 第三个是\w位,其后是将其变为蓝色的颜色信息。 第四个是\$位,其后是删除所有颜色的颜色信息。

Once you understand how to construct color tags of your own, you can add whatever colors you like to whatever sections of your Bash prompt you like.

一旦了解了如何构造自己的颜色标签,就可以在Bash提示的任何部分添加所需的颜色。

Here’s what you need to know: You must include the entire color code information between the \[  and \] characters. Inside the tag, you must begin with either \033[ or \e[ to indicate to Bash that this is color information. Both \033[ and \e[ do the same thing. \e[ is shorter so might be more convenient to use, but we’ll use \033[ here as it matches what’s used by default. At the end of the tag, you must end with m\ to indicate the end of a color tag.

这是您需要了解的内容:您必须在\[\] character之间包括整个颜色代码信息。 在标签内,您必须以\033[\e[开头,以向Bash指示这是颜色信息。 \033[\e[都做同样的事情。 \e[较短,因此可能更方便使用,但是我们将在这里使用\033[ ,因为它与默认情况下的匹配。 在标签的末尾,您必须以m\结尾以指示颜色标签的末尾。

Breaking that down, here’s what every color tag will look like. The only difference is the information you add in place of COLOR to define the actual color:

分解一下,这就是每个颜色标签的外观。 唯一的区别是您添加的信息代替了COLOR来定义实际的颜色:

\[\033[COLORm\]

Bash allows you to change the color of foreground text, add attributes like “bold” or “underline” to the text, and set a background color.

Bash允许您更改前景文本的颜色,向文本添加“粗体”或“下划线”之类的属性,并设置背景颜色。

Here are the values for foreground text:

这是前景文本的值:

  • Black: 30

    黑色:30
  • Blue: 34

    蓝色:34
  • Cyan: 36

    青色:36
  • Green: 32

    绿色:32
  • Purple: 35

    紫色:35
  • Red: 31

    红色:31
  • White: 37

    白色:37
  • Yellow: 33

    黄色:33

For example, since purple text is color code 32, you’d use \[\033[32m\]  for purple text.

例如,由于紫色文本的颜色代码为32,因此您将对紫色文本使用\[\033[ 32 m\]

You can also specify an attribute for the text. This attribute must be added before the color number,  separated by a semicolon (;). Text with these attributes will look different in different terminal emulators.

您还可以为文本指定属性。 必须在颜色编号之前添加此属性,并用分号(;)分隔。 具有这些属性的文本在不同的终端仿真器中看起来会有所不同。

Here are the values for text attributes:

这是文本属性的值:

  • Normal Text: 0

    普通文字:0
  • Bold or Light Text: 1 (It depends on the terminal emulator.)

    粗体或浅色文本:1(取决于终端仿真器。)
  • Dim Text: 2

    昏暗的文字:2
  • Underlined Text: 4

    带下划线的文字:4
  • Blinking Text: 5 (This does not work in most terminal emulators.)

    闪烁文本:5(在大多数终端仿真器中不起作用。)
  • Reversed Text: 7 (This inverts the foreground and background colors, so you’ll see black text on a white background if the current text is white text on a black background.)

    反转文本:7(这会反转前景色和背景色,因此,如果当前文本是黑色背景上的白色文本,则会在白色背景上看到黑色文本。)
  • Hidden Text: 8

    隐藏文字:8

You don’t actually need to include the normal text attribute. That’s the default, anyway.

您实际上不需要包括普通的text属性。 无论如何,这是默认设置。

For example, since red text is code 31 and bold text is code 1, you’d use \[\033[1;31m\] for bold red text.

例如,由于红色文本为代码31,粗体文本为代码1,因此对于粗体红色文本,请使用\[\033[ 1;31 m\]

You can also specify a background color, but you can’t add an attribute to a background color.

您也可以指定背景色,但不能将属性添加到背景色。

Here are the values for background colors:

以下是背景色的值:

  • Black background: 40

    黑色背景:40
  • Blue background: 44

    蓝色背景:44
  • Cyan background: 46

    青色背景:46
  • Green background: 42

    绿色背景:42
  • Purple background: 45

    紫色背景:45
  • Red background: 41

    红色背景:41
  • White background: 47

    白色背景:47
  • Yellow background: 43

    黄色背景:43

For example, since a blue background is code 44, \[\033[44m\] would specify a blue background.

例如,由于蓝色背景为代码44, \[\033[ 44 m\]将指定蓝色背景。

You can specify both foreground and background color tags. For example, 42 represents a green background and 31 represents red text. So, to make the default prompt become red text on a green background, you’d use:

您可以同时指定前景色和背景色标签。 例如,42代表绿色背景,31代表红色文本。 因此,要使默认提示在绿色背景上变为红色文本,可以使用:

PS1="\[\033[42m\]\[\033[31m\]\u@\h:\w\$ "

We just specify a single background color and then a single foreground text color here, which begins at the start of the prompt and is applied to all text in the prompt. However, you can specify as many color tags as you want in the variable to color different sections of your prompt however you like.

我们仅在此处指定一种背景颜色,然后指定一种前景文本颜色,该颜色从提示的开头开始,并应用于提示中的所有文本。 但是,您可以在变量中指定任意数量的颜色标签,以根据需要为提示符的不同部分上色。

The background and foreground text colors keep going past the prompt unless you specify color code 00 clear the color information. You can also use this tag within the variable to reset formatting back to default somewhere in your prompt. For example,  the following line would end all coloring before the \$ character.

除非您指定颜色代码00清除颜色信息,否则背景和前景文本的颜色将一直超出提示。 您还可以在变量中使用此标记,以将格式重置为提示中的默认位置。 例如,以下行将结束\$字符之前的所有着色。

PS1="\[\033[42m\]\[\033[31m\]\u@\h:\w\\[\033[00m\]\$ "

如何设置新的默认提示 (How to Set Your New Default Prompt)

Once you’re done experimenting with colors, you should have a Bash prompt you like in the current session. But you probably want to make that new prompt permanent so it’s automatically used in all your Bash sessions.

完成颜色实验后,您应该在当前会话中看到一个喜欢的Bash提示。 但是您可能希望使该新提示永久保留,以便在所有Bash会话中自动使用它。

To do this, you just need to change the contents of the PS1 variable in the .bashrc file, which we looked at earlier.

为此,您只需要更改.bashrc文件中的PS1变量的内容即可,我们之前已经看过它。

Open the .bashrc file in your preferred text editor, like so:

在您喜欢的文本编辑器中打开.bashrc文件,如下所示:

nano ~/.bashrc

Scroll down and locate the PS1= section. Just replace the default variable with your customized variable. You’ll probably want to leave the ${debian_chroot:+($debian_chroot)}  bits alone, however—they won’t appear unless you’re in a chroot environment, anyway.

向下滚动并找到PS1 =部分。 只需将默认变量替换为您的自定义变量即可。 但是,您可能想单独保留${debian_chroot:+($debian_chroot)}位-除非您处于chroot环境中,否则它们不会出现。

Enter your colored PS1 variable under the if [ "$color_prompt" = yes ]; then line. Enter the variable without colors under the else line.

if [ "$color_prompt" = yes ]; then下输入您的彩色PS1变量if [ "$color_prompt" = yes ]; then if [ "$color_prompt" = yes ]; then行。 在else行下输入不带颜色的变量。

Save the file and close your text editor. For example, to save the file in nano, press Ctrl+O, press Enter, and then press Ctrl+X to exit.

保存文件并关闭文本编辑器。 例如,要将文件保存为nano,请按Ctrl + O,按Enter,然后按Ctrl + X退出。

The next time you start a new Bash shell—for example, by signing in at the terminal or by opening a new terminal window—you’ll see your customized prompt.

下次启动新的Bash shell时(例如,通过在终端上登录或打开新的终端窗口),您将看到自定义的提示。

翻译自: https://www.howtogeek.com/307701/how-to-customize-and-colorize-your-bash-prompt/

linux 自定义bash

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值