Linux中三种添加环境变量的方法与区别

时间:2025-03-21 07:11:02

Linux中三种添加环境变量的方法与区别

  • 1. 终端中直接用export添加
  • 2. 在路径文件/root/.bash_profile中添加路径
  • 3. 在路径文件/etc/profile中添加路径

1. 终端中直接用export添加

[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin

# 添加路径
[root@localhost ~]# export PATH="$PATH:/opt/pgsql-15.3/bin"
 
# 路径生效
[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:/opt/pgsql-15.3/bin
[root@localhost ~]# postgres --version
postgres (PostgreSQL) 15.3

# 这种修改方式只对当前用户有效
[root@localhost ~]# su - user
[user@localhost ~]$ postgres --version
bash: postgres:command not found

# 这种修改方式只对当前会话有效,也就是说每当登出或注销系统以后,PATH设置就会失效
[usert@localhost ~]$ su - root
password:
[root@localhost ~]# postgres --version
bash: postgres:command not found
  • 总结:这种修改方式是仅限于当前会话的,当前会话登出或注销,对$PATH的修改也就失效了

2. 在路径文件/root/.bash_profile中添加路径

[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin

# 添加路径
[root@localhost ~]# cat /root/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin	# 在这一行添加新的路径

export PATH
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# vim /root/.bash_profile
# 修改完后执行cat 
[root@localhost ~]# cat /root/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/opt/pgsql-15.3/bin	# 在这一行添加了新的路径

export PATH
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 

# 当前会话路径生效
[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:/opt/pgsql-15.3/bin
[root@localhost ~]# postgres --version
postgres (PostgreSQL) 15.3

# 这种修改方式只对root用户生效,普通用户无效
[root@localhost ~]# su - user
[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 --version
postgres (PostgreSQL) 15.3
  • 总结:这种修改方式仅仅对当前用户root的所有会话永久有效,切换系统用户后,无效

3. 在路径文件/etc/profile中添加路径

[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
[root@localhost ~]# vim /etc/profile
[root@localhost ~]# cat /etc/profile
# /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  shell script in
# /etc// 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
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
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										# 在这一行添加了新的路径

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
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 ~]# source /etc/profile
[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:/opt/pgsql-15.3/bin
[root@localhost ~]# postgres --version
postgres (PostgreSQL) 15.3

# 切换用户后仍然生效
[root@localhost ~]# su - userc
上一次登录:五 69 14:09:51 CST 2023pts/0 上
[userc@localhost ~]$ postgres --version
postgres (PostgreSQL) 15.3
  • 总结:这种修改方式对所有系统用户都永久生效