深入探索Shell配置与环境变量
立即解锁
发布时间: 2025-09-09 01:47:57 阅读量: 14 订阅数: 21 AIGC 


从零开始学Linux系统管理
# 深入探索Shell配置与环境变量
## 1. 全局与本地配置文件
### 1.1 全局配置文件 /etc/bashrc
`/etc/bashrc` 的作用是设置系统范围的函数和别名,包括终端模拟器类型、命令提示符字符串、umask(定义新文件创建时的默认权限)以及 `$SHELL` 变量(定义bash shell可执行文件的全限定路径和名称)。
需要注意的是,用于bash shell全局配置的默认文件不应被修改。若要修改或添加全局配置,应在 `/etc/profile.d` 目录中添加自定义文件,文件名以 `.sh` 结尾即可,建议取一个容易识别的名字。
### 1.2 本地配置文件
本地bash配置文件位于每个用户的主目录下,用户可修改这些文件以根据自己的偏好配置shell环境。主要的本地配置文件有 `.bashrc` 和 `.bash_profile`,它们包含一些基本的配置项。
当启动登录shell时,bash首先运行 `/etc/profile`,完成后运行 `~/.bash_profile`。以下是 `~/.bash_profile` 和 `~/.bashrc` 的内容示例:
```bash
# ~/.bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
```
```bash
# ~/.bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging
feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
```
`~/.bash_profile` 先运行 `~/.bashrc` 以设置别名和函数,然后设置并导出路径,使该路径对所有后续的非登录shell可用。`~/.bashrc` 会调用 `/etc/bashrc`。
### 1.3 配置文件执行顺序
登录shell启动时的执行顺序如下:
```mermaid
graph LR
A[/etc/profile] --> B[~/.bash_profile]
B --> C[~/.bashrc]
C --> D[/etc/bashrc]
```
## 2. 追踪配置脚本执行顺序
为了测试复杂且相互关联的shell程序或shell程序内过程的执行顺序,可以在每个相关程序的开头添加 `echo` 语句,以显示正在运行的shell程序。
### 2.1 修改配置文件
以下是需要修改的文件及具体操作:
- **/etc/profile**:在 `pathmunge` 过程之后添加 `echo "Running /etc/profile"`。
```bash
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
echo "Running /etc/profile"
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`id -u`
UID=`id -ru`
fi
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
```
- **/etc/bashrc**:在文件开头添加 `echo "Running /etc/bashrc"`。
```bash
# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
echo "Running /etc/bashrc"
# Prevent doublesourcing
if [ -z ".bashrcSOURCED" ]; then
.bashrcSOURCED="Y"
```
- **/etc/profile.d/myBashConfig.sh**:创建该文件并添加 `echo "Running /etc/profile.d/myBashConfig.sh"`。
```bash
# /etc/profile.d/myBashConfig.sh
echo "Running /etc/profile.d/myBashConfig.sh"
```
- **~/.bash_profile**:在文件开头添加 `echo "Running ~/.bash_profile"`。
```bash
# .bash_profile
echo "Running ~/.bash_profile"
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
```
- **~/.bashrc**:在文件开头添加 `echo "Running ~/.bashrc"`。
```bash
# .bashrc
echo
```
0
0
复制全文
相关推荐










