Linux.bashrc

环境变量

1
2
# Erlang
# export PATH=/opt/otp/bin:$PATH

Ubuntu 终端颜色显示 PS1 bash 输出文本颜色配置

  • 突然发现终端文件不高亮显示,可以修改 .bashrc 文件来调节

  • 将以下加入到 .bashrc 的最后

1
PS1='\[\e[1;36m\]\d \[\e[1;32m\]\t \[\e[1;33m\]\u@\[\e[1;35m\]\h:\w\$\[\e[0;31m\] '

退出终端重新登陆或执行以下命令可看到效果

1
source .bashrc

其它的一些参考的色彩设置:

1
2
3
4
5
6
7
$ 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"

当前服务器配置

1
2
3
4
5
6
# 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\] '

其中配置项

1
PS1='\[\e[1;36m\]\d \[\e[1;32m\]\t \[\e[1;33m\]\u@\[\e[1;35m\]\h:\w\$\[\e[0;37m\] '

显示效果为

1
Fri Feb 26 11:55:20 yzhang@coam:~$

Linux PS1 配置对应表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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 表示背景颜色

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名称

1
2
3
4
5
6
7
# 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

文章目录
  1. 1. 环境变量
  2. 2. Ubuntu 终端颜色显示 PS1 bash 输出文本颜色配置
  • 退出终端重新登陆或执行以下命令可看到效果
    1. 1. 当前服务器配置
    2. 2. 其中配置项
    3. 3. 显示效果为
      1. 3.1. Linux PS1 配置对应表
    4. 4. 使用 Putty 客户端登陆后显示对应的服务器ip名称