注册
Linux环境变量
专栏/技术分享/ 文章详情 /

Linux环境变量

zxw 2026/08/28 105 0 0
摘要

​ .bash_profile 和 .bashrc 都是 Linux Bash Shell 的启动配置文件,作用都是设置用户环境,但是加载时机不同

文件 作用 什么时候执行
.bash_profile 登录环境配置 用户登录时执行
.bashrc Shell交互环境配置 每次打开一个新的 Bash 终端时执行

这两个文件加载时机的区别:

(1)登录 Shell(login shell)

例如:

ssh oracle@192.168.1.10

或者是:

su - oracle

流程是这样的:

bash启动
 |
 |--读取 /etc/profile
 |
 |--读取 ~/.bash_profile
 |
 |--结束

(2)非登录 Shell(non-login shell)

例如:

su oracle

流程是这样的:

bash启动
 |
 |--读取 ~/.bashrc
 |
 |--结束

这两种文件的格式:

# .bash_profile文件


[ -f ~/.bashrc ] && . ~/.bashrc
# Oracle Environment
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe
export ORACLE_SID=XE
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
# .bashrc文件

# Source default setting
[ -f /etc/bashrc ] && . /etc/bashrc

# User environment PATH
export PATH

修改完用户变量文件后需要执行一下:

source /home/oracle/.bash_profile

才能立即生效。

问题1:当你source后可以用echo $ORACLE_HOME命令检查到设置了用户变量,但su - root,su -

oracle 切换一下,回来用户变量就失效了。

​ 检查一下你登录是加载的家目录下的.bash_profile文件是不是你设置的那个,比如:

# 以长格式列出家目录下的.bash_profile文件
[oracle@192 ~]$ ls -l ~/.bash_profile
ls: 无法访问 '/u01/app/oracle/.bash_profile': 没有那个文件或目录

说明家目录不是/home/oracle,那修改/home/oracle/.bash_profile自然不会被加载到,解决方法:

# 修改oracle家目录
usermod -d /home/oracle oracle 

# 修改目录权限
chown oracle:dba /home/oracle
评论
后发表回复

作者

文章

阅读量

获赞

扫一扫
联系客服