bashrc文件解释

一、.bashrc 文件作用说明

.bashrcBash 非登录 shell 的启动配置文件(比如终端模拟器打开的 shell),每次打开交互式非登录 shell 时会自动执行,用于配置 shell 的环境变量、别名、提示符样式、补全功能等。

二、文件内容逐段解析

1. 非交互式 shell 直接退出

1
2
3
4
case $- in
*i*) ;;
*) return;;
esac
  • $- 是 shell 的标志变量,i 表示交互式 shell(用户可以输入命令)。
  • 如果是非交互式 shell(比如脚本执行),直接return,不加载后续配置,提升效率。

2. 历史记录配置

1
2
3
4
HISTCONTROL=ignoreboth  # 忽略重复行、以空格开头的行(避免敏感命令入历史)
shopt -s histappend # 历史记录追加到文件(而非覆盖)
HISTSIZE=1000 # 内存中保存的历史命令数
HISTFILESIZE=2000 # 历史文件(~/.bash_history)中保存的命令数

3. 终端与路径匹配配置

1
2
3
shopt -s checkwinsize   # 每次命令后检查窗口大小,更新LINES/COLUMNS
# shopt -s globstar # 注释:** 匹配所有文件/子目录(比如ls **/*.txt递归匹配)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" # less支持非文本文件预览

4. Debian chroot 环境识别

1
2
3
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
  • 若处于 chroot 环境,提示符会显示 chroot 名称,便于区分环境。

5. 彩色提示符配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# force_color_prompt=yes # 注释:强制开启彩色提示符(取消注释可强制生效)
if [ -n "$force_color_prompt" ]; then
# 检测终端是否支持颜色
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
color_prompt=yes
else
color_prompt=
fi
fi

# 彩色提示符格式:用户@主机:工作目录$
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
  • 颜色码:01;32m(绿色,加粗)、01;34m(蓝色,加粗)、00m(重置颜色)。
  • \u:当前用户,\h:主机名,\w:当前工作目录(绝对路径)。

6. Xterm 终端标题设置

1
2
3
4
5
6
7
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
  • 把 Xterm 终端的标题设为用户@主机: 工作目录,便于识别多个终端窗口。

7. ls/grep 颜色支持

1
2
3
4
5
6
7
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto' # ls带颜色
alias grep='grep --color=auto' # grep匹配结果带颜色
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
  • dircolors:设置 ls 的颜色规则(可通过~/.dircolors自定义)。

8. 常用别名与 alert 提示

1
2
3
4
5
6
7
# ls别名
alias ll='ls -alF' # 详细列表(隐藏文件+格式后缀)
alias la='ls -A' # 显示所有文件(排除.和..)
alias l='ls -CF' # 分栏显示+格式后缀

# 长命令结束后通知(比如:sleep 10; alert)
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

9. 加载独立的别名文件

1
2
3
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
  • 推荐将自定义别名放在~/.bash_aliases(而非直接写在.bashrc),便于维护。

10. 启用 Bash 补全功能

1
2
3
4
5
6
7
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
  • 启用命令补全(比如输入git 后按 Tab 键补全子命令),提升操作效率。

11. 自定义环境变量(嵌入式开发相关)

1
2
3
4
5
6
7
8
# ARM交叉编译环境配置
export ARCH=arm # 指定架构为ARM
export CROSS_COMPILE=arm-buildroot-linux-gnueabihf- # 交叉编译器前缀
# 添加交叉编译器路径到系统PATH
export PATH=$PATH:/home/dky/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin

# GTK3的pkg-config搜索路径
export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig:$PKG_CONFIG_PATH
  • 交叉编译:针对 IMX6ULL 开发板的 ARM 架构交叉编译环境,配置后可直接使用arm-buildroot-linux-gnueabihf-gcc等工具。
  • pkg-config:用于编译依赖 GTK3 的程序时,让 pkg-config 找到 GTK3 的.pc 配置文件。