用这个简单的技巧为你的 Bash 提示着色

当您第一次打开终端应用程序时,您会看到一个提示,告诉您您的用户名、机器名称以及可能的其他信息,例如纯白色文本的当前目录。 然而,在 Manjaro Linux 中,Manjaro 开发团队将提示修改为看起来更愉快。 但是,您不能修改有关界面的任何内容(颜色等)。 在本文中,我将向您展示如何着色 bash 通过简单的 bashrc 修改轻松提示。

Shell 脚本 101:Shell 脚本中的选择循环

检查您正在使用哪个外壳

默认情况下,所有主要发行版通常都附带 Bash,但在您的 Linux 发行版中可能会有所不同,或者如果您安装了新的 shell,例如 FISH 或 KSH。 要识别正在使用的外壳,只需打开终端并键入:

ps $$
识别正在使用的外壳

输出将仅显示有关您当前正在使用的 shell 的信息。

备份您的 BahsRC / FishRC / ZhRC 文件

备份旧文件很重要,以防万一我们搞砸了,我们的系统开始出现异常。 要为您的 shell(例如 ZhRC、FishRC 等)执行此操作,请键入以下命令:

cp .bashrc bash.bak

或者

cp .zshrc zsh.bak

同样对于 K Shell :

cp .kshrc ksh.bak

修改你的 Bash 文件

使用您喜欢的文本编辑器(例如 Nano 或 Vim)打开 .bashrc(或您的 shell 文件)文件。 只需键入:

nano .bashrc

或者,如果您使用 Vim 文本编辑器:

vim .bashrc
打开 .bashrc 文件

现在,导航到底部并添加以下脚本:

#    Original Author: Caesar Kabalan
#    Modifications : Aadesh
#    Where:
#       CWD - Current working directory (green if last command returned 0, red otherwise)
#       COUNT - Session command count
#       BRANCH - Current git branch if in a git repository, omitted if not in a git repo
#       VENV - Current Python Virtual Environment if set, omitted if not set
#       USER - Current username
#       HOSTNAME - System hostname
function set_bash_prompt () {
	# Color codes for easy prompt building
	COLOR_DIVIDER="[e[30;1m]"
	COLOR_CMDCOUNT="[e[36;1m]"
	COLOR_USERNAME="[e[33;1m]"
	COLOR_DIVIDER="[e[31;1m]"
	COLOR_USERHOSTAT="[e[31;1m]"
	COLOR_HOSTNAME="[e[33;1m]"
	COLOR_GITBRANCH="[e[33;1m]"
	COLOR_VENV="[e[33;1m]"
	COLOR_GREEN="[e[32;1m]"
	COLOR_PATH_OK="[e[32;1m]"
	COLOR_PATH_ERR="[e[31;1m]"
	COLOR_NONE="[e[0m]"
	# Change the path color based on return value.
	if test $? -eq 0 ; then
		PATH_COLOR=${COLOR_PATH_OK}
	else
		PATH_COLOR=${COLOR_PATH_ERR}
	fi
	# Set the PS1 to be "[workingdirectory:commandcount"
	PS1="${COLOR_DIVIDER}[${PATH_COLOR}w${COLOR_DIVIDER}:${COLOR_CMDCOUNT}#${COLOR_DIVIDER}"
	# Add git branch portion of the prompt, this adds ":branchname"
	if ! git_loc="$(type -p "$git_command_name")" || [ -z "$git_loc" ]; then
		# Git is installed
		if [ -d .git ] || git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
			# Inside of a git repository
			GIT_BRANCH=$(git symbolic-ref --short HEAD)
			PS1="${PS1}:${COLOR_GITBRANCH}${GIT_BRANCH}${COLOR_DIVIDER}"
		fi
	fi
	# Add Python VirtualEnv portion of the prompt, this adds ":venvname"
	if ! test -z "$VIRTUAL_ENV" ; then
		PS1="${PS1}:${COLOR_VENV}`basename "$VIRTUAL_ENV"`${COLOR_DIVIDER}"
	fi
	# Close out the prompt, this adds "]n[username@hostname] "
	PS1="${PS1}]n${COLOR_DIVIDER}[${COLOR_USERNAME}u${COLOR_USERHOSTAT}@${COLOR_HOSTNAME}h${COLOR_DIVIDER}]${COLOR_NONE} "
}

# Tell Bash to run the above function for every prompt
export PROMPT_COMMAND=set_bash_prompt

# tab completion
if ! shopt -oq posix; then
	if [ -f /usr/share/bash-completion/bash_completion ]; then
		. /usr/share/bash-completion/bash_completion
	elif [ -f /etc/bash_completion ]; then
		. /etc/bash_completion
	fi
fi

# use colours in the prompt
[[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
[[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
[[ -z ${match_lhs} ]] 
	&& type -P dircolors >/dev/null 
	&& match_lhs=$(dircolors --print-database)

if [[ $'n'${match_lhs} == *$'n'"TERM "${safe_term}* ]] ; then

	if type -P dircolors >/dev/null ; then
		if [[ -f ~/.dir_colors ]] ; then
			eval $(dircolors -b ~/.dir_colors)
		elif [[ -f /etc/DIR_COLORS ]] ; then
			eval $(dircolors -b /etc/DIR_COLORS)
		fi
	fi

	PS1="$(if [[ ${EUID} == 0 ]]; then echo '[33[01;31m]h'; else echo '[33[01;32m]u@h'; fi)[33[01;34m] w $([[ $? != 0 ]] && echo "[33[01;31m]:([33[01;34m] ")\$[33[00m] "

else

	PS1="u@h w $([[ $? != 0 ]] && echo ":( ")$ "

fi

粘贴此代码后,如果您使用 Nano 文本编辑器保存文件,只需按 Ctrl+O,然后按 Ctrl+X 退出。 如果您使用的是 Vim,请输入 :wq 并按 enter 保存并退出。

Save  和 Exit 来自 Bashrc 文件Save 和 Exit 来自 Bashrc 文件

最后,让我们重新加载我们的 bash 以便它识别我们最近的修改:

source .bashrc
更新后的 Bash Prompt 看起来像这样更新后的 Bash 提示看起来像这样

如果您不喜欢我搭配的颜色,您也可以更改颜色。 使用本文中提到的 ANSI 代码表。

参考

感谢 GitHub 上的原始代码作者 Caeser Kabalan。 我们刚刚将此代码修改为我们喜欢的颜色首选项。