MENU

bash样式推荐

January 24, 2019 • Linux阅读设置

前言

一个好的 bash 样式会使工作更加便捷,更加赏心悦目,博主推荐几个比较好看和实用的样式,各位有需要的可以用用看

如何使用

  • 将代码加入到用户家目录下面的.bashrc 文件中
只对当前用户生效,如果需要对所有用户生效可以加到全局配置文件中

样式

[主机名_用户名:当前目录 (相对路径)- 当前时间]:

export PS1="\[\e[31m\][\[\e[m\]\[\e[34m\]\h\[\e[m\]\[\e[36m\]_\[\e[m\]\[\e[31m\]\u\[\e[m\]\[\e[36m\]:\[\e[m\]\[\e[35m\]\W\[\e[m\]\[\e[36m\]-\[\e[m\]\[\e[33m\]\T\[\e[m\]\[\e[31m\]]\[\e[m\]\[\e[36m\]:\[\e[m\] "

[用户名 @主机名_当前目录 (绝对路径)]:

export PS1="\[\e[36m\][\[\e[m\]\[\e[34m\]\u\[\e[m\]\[\e[36m\]@\[\e[m\]\[\e[31m\]\h\[\e[m\]\[\e[36m\]_\[\e[m\]\[\e[33m\]\w\[\e[m\]\[\e[36m\]]\[\e[m\]\[\e[36m\]:\[\e[m\] "

[用户名 @主机名 & err = 错误代码]:

错误代码栏只显示上一次执行的错误退出

function nonzero_return() {
RETVAL=$?
[ $RETVAL -ne 0 ] && echo "$RETVAL"
}
export PS1="\[\e[34m\][\[\e[m\]\[\e[36m\]\u\[\e[m\]\[\e[36m\]@\[\e[m\]\[\e[33m\]\h\[\e[m\]\[\e[31m\]&err=\[\e[m\]\[\e[31m\]\`nonzero_return\`\[\e[m\]\[\e[34m\]]\[\e[m\]\[\e[34m\]:\[\e[m\] "

用户名 @主机 / 当前目录 (绝对路径)/git 状态:

实际效果没有 / 符号,git 状态只有进入到 git 目录才会显示

# get current branch in git repo
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
echo "[${BRANCH}${STAT}]"
else
echo ""
fi
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " ${bits}"
else
echo ""
fi
}
export PS1="\[\e[31m\]\u\[\e[m\]\[\e[36m\]@\[\e[m\]\[\e[34m\]\h\[\e[m\]\[\e[33m\]\w\[\e[m\]\[\e[35m\]\`parse_git_branch\`\[\e[m\]\[\e[36m\]:\[\e[m\] "

最后编辑于: June 5, 2019
Archives QR Code
QR Code for this page
Tipping QR Code