Linux Bashrc 配置
Summary: Author: 张亚飞 | Read Time: 2 minute read | Published: 2015-08-17
Filed under
—
Categories:
MarkDown
—
Tags:
Tag,
环境变量
# Erlang
# export PATH=/opt/otp/bin:$PATH
Ubuntu 终端颜色显示 PS1 bash 输出文本颜色配置
突然发现终端文件不高亮显示,可以修改 .bashrc 文件来调节
将以下加入到 .bashrc 的最后
PS1='\[\e[1;36m\]\d \[\e[1;32m\]\t \[\e[1;33m\]\u@\[\e[1;35m\]\h:\w\$\[\e[0;31m\] '
退出终端重新登陆或执行以下命令可看到效果
source .bashrc
其它的一些参考的色彩设置:
$ PS1='\[\e[1;34m\][\d \t \u@\h \w]\$\[\e[m\] '
$ PS1='\[\e[1;36m\]\d \[\e[1;32m\]\t \[\e[1;33m\]\u@\[\e[1;35m\]\h:\w\$\[\e[0;31m\] '
$ PS1='\[\e[1;33m\]\u@\h \w ->\n\[\e[1;36m\] \@ \d\$\[\e[m\] '
$ PS1="[\[\e[0;32m\]\u@\h, load: `cat /proc/loadavg | awk '{ print $1; }'`\[\e[00m\]] (\[\e[00;35m\]\d - \t\[\e[00m\])\n\w \$ "
$ PS1="[\u@\h, load: `cat /proc/loadavg | awk '{ print $1; }'`] (\d - \t)\n\w \$ "
$ PS1="$HC$FYEL[ $FBLE${debian_chroot:+($debian_chroot)}\u$FYEL: $FBLE\w $FYEL]\\$ $RS"
$ PS2="$HC$FYEL> $RS"
当前服务器配置
# Terminate HighLight
#PS1='${debian_chroot:+($debian_chroot)}\[\033[01;35;40m\]\u\[\033[00;00;40m\]@\[\033[01;35;40m\]\h\[\033[00;31;40m\]:\[\033[00;00;40m\]\w \[\033[01;32;40m\]\$ \[\033[01;36;40m\]'
#PS1="$HC$FYEL[ $FBLE${debian_chroot:+($debian_chroot)}\u$FYEL: $FBLE\w $FYEL]\\$ $RS"
#PS2="$HC$FYEL> $RS"
#PS1='\[\e[1;33m\]\u@\h \w ->\n\[\e[1;36m\] \@ \d\$\[\e[m\] '
PS1='\[\e[1;36m\]\d \[\e[1;32m\]\t \[\e[1;33m\]\u@\[\e[1;35m\]\h:\w\$\[\e[0;37m\] '
其中配置项
PS1='\[\e[1;36m\]\d \[\e[1;32m\]\t \[\e[1;33m\]\u@\[\e[1;35m\]\h:\w\$\[\e[0;37m\] '
显示效果为
Fri Feb 26 11:55:20 coam@coam:~$
Linux PS1 配置对应表
# ANSI color codes
RS="\[\033[0m\]" # reset
HC="\[\033[1m\]" # hicolor
UL="\[\033[4m\]" # underline
INV="\[\033[7m\]" # inverse background and foreground
FBLK="\[\033[30m\]" # foreground black
FRED="\[\033[31m\]" # foreground red
FGRN="\[\033[32m\]" # foreground green
FYEL="\[\033[33m\]" # foreground yellow
FBLE="\[\033[34m\]" # foreground blue
FMAG="\[\033[35m\]" # foreground magenta
FCYN="\[\033[36m\]" # foreground cyan
FWHT="\[\033[37m\]" # foreground white
BBLK="\[\033[40m\]" # background black
BRED="\[\033[41m\]" # background red
BGRN="\[\033[42m\]" # background green
BYEL="\[\033[43m\]" # background yellow
BBLE="\[\033[44m\]" # background blue
BMAG="\[\033[45m\]" # background magenta
BCYN="\[\033[46m\]" # background cyan
BWHT="\[\033[47m\]" # background white
注:每种颜色都有两种,分别以 3* 和 4* 开头的数字配置,3* 表示文字颜色,4* 表示背景颜色
Reference:
- CustomizingBashPrompt
- http://bashrcgenerator.com/#minidoc
- How to Customize your Bash Prompt on a Linux VPS
- How to Make a Fancy and Useful Bash Prompt in Linux
使用 Putty 客户端登陆后显示对应的服务器ip名称
# Auto add env parameter $PROMPT_COMMAND when use non-Linux tty login by ssh.
# 让 putty 登陆的标题栏显示对应服务器 IP
if [ "$SSH_CONNECTION" != '' -a "$TERM" != 'linux' ]; then
declare -a HOSTIP
HOSTIP=`echo $SSH_CONNECTION |awk '{print $3}'`
export PROMPT_COMMAND='echo -ne "\033]0;${USER}@$HOSTIP:[${HOSTNAME%%.*}]:${PWD/#$HOME/~} \007"'
fi
最后附加上所有添加到服务器的 .bashrc 完成配置
# Erlang
#export PATH=/opt/otp/bin:$PATH
# Terminate HighLight
#PS1='${debian_chroot:+($debian_chroot)}\[\033[01;35;40m\]\u\[\033[00;00;40m\]@\[\033[01;35;40m\]\h\[\033[00;31;40m\]:\[\033[00;00;40m\]\w \[\033[01;32;40m\]\$ \[\033[01;36;40m\]'
#PS1="$HC$FYEL[ $FBLE${debian_chroot:+($debian_chroot)}\u$FYEL: $FBLE\w $FYEL]\\$ $RS"
#PS2="$HC$FYEL> $RS"
#PS1='\[\e[1;33m\]\u@\h \w ->\n\[\e[1;36m\] \@ \d\$\[\e[m\] '
PS1='\[\e[1;36m\]\d \[\e[1;32m\]\t \[\e[1;33m\]\u@\[\e[1;35m\]\h:\w\$\[\e[0;37m\] '
# Auto add env parameter $PROMPT_COMMAND when use non-Linux tty login by ssh.
# 让 putty 登陆的标题栏显示对应服务器 IP
if [ "$SSH_CONNECTION" != '' -a "$TERM" != 'linux' ]; then
declare -a HOSTIP
HOSTIP=`echo $SSH_CONNECTION |awk '{print $3}'`
export PROMPT_COMMAND='echo -ne "\033]0;${USER}@$HOSTIP:[${HOSTNAME%%.*}]:${PWD/#$HOME/~} \007"'
fi
修改用户主目录下的 .bashrc 文件后每次登陆都需要手动执行命令 source .bashrc 才能生效问题
原因是用户主目录下没有 .profile 文件,可以从其他服务器拷贝一份副本过来(无需修改),大概内容为
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
再次登录,通过先执行.profile再使.bashrc生效
参考 ubuntu 用户修改.bashrc之后,每次登录需要运行source命令才生效
vi .bash_profile
# 设置 ls 文件夹列表样式
export CLICOLOR=1
Comments