Linux中三种添加环境变量的方法与区别
- 1. 终端中直接用export添加
- 2. 在路径文件/root/.bash_profile中添加路径
- 3. 在路径文件/etc/profile中添加路径
1. 终端中直接用export添加
[root@localhost ~]
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
[root@localhost ~]
[root@localhost ~]
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:/opt/pgsql-15.3/bin
[root@localhost ~]
postgres (PostgreSQL) 15.3
[root@localhost ~]
[user@localhost ~]$ postgres --version
bash: postgres:command not found
[usert@localhost ~]$ su - root
password:
[root@localhost ~]
bash: postgres:command not found
- 总结:这种修改方式是仅限于当前会话的,当前会话登出或注销,对
$PATH
的修改也就失效了
2. 在路径文件/root/.bash_profile中添加路径
[root@localhost ~]
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
[root@localhost ~]
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
PATH=$PATH:$HOME/bin
export PATH
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
PATH=$PATH:$HOME/bin:/opt/pgsql-15.3/bin
export PATH
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:/opt/pgsql-15.3/bin
[root@localhost ~]
postgres (PostgreSQL) 15.3
[root@localhost ~]
[user@localhost ~]$ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
[usert@localhost ~]$ su - root
password:
[root@localhost ~]
postgres (PostgreSQL) 15.3
- 总结:这种修改方式仅仅对当前用户root的所有会话永久有效,切换系统用户后,无效
3. 在路径文件/etc/profile中添加路径
[root@localhost ~]
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
[root@localhost ~]
[root@localhost ~]
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
EUID=`/usr/bin/id -u`
UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
export PATH=$PATH:/opt/pgsql-15.3/bin
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
for i in /etc//*.sh /etc// ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
[root@localhost ~]
[root@localhost ~]
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:/opt/pgsql-15.3/bin
[root@localhost ~]
postgres (PostgreSQL) 15.3
[root@localhost ~]
上一次登录:五 6月 9 14:09:51 CST 2023pts/0 上
[userc@localhost ~]$ postgres --version
postgres (PostgreSQL) 15.3