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

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 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
}


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/profile.d/*.sh /etc/profile.d/sh.local ; 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
  • 总结:这种修改方式对所有系统用户都永久生效
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统,PATH环境变量是一个非常重要的变量,它指示系统在何处查找可执行文件。在命令行输入命令时,系统会根据PATH变量指定的路径去查找该命令的可执行文件。下面介绍三种方法来设置PATH环境变量。 第一种方法:通过export命令设置PATH环境变量 可以使用export命令来设置PATH环境变量,例如: $ export PATH=/home/user/bin:/usr/local/bin:$PATH 这个命令会将/home/user/bin和/usr/local/bin路径添加到PATH环境变量。在这个例子,我们将这两个路径添加到了PATH环境变量的前面,这意味着系统会先在这些路径查找可执行文件,然后再在其他路径查找。 第二种方法:通过修改.bashrc文件设置PATH环境变量Linux系统,用户的个人shell配置文件为~/.bashrc。可以直接编辑这个文件,并在其添加PATH环境变量的设置,例如: $ vi ~/.bashrc export PATH=/home/user/bin:/usr/local/bin:$PATH 在保存修改后,要使用新的PATH环境变量,需要重新启动终端或执行以下命令: $ source ~/.bashrc 第三种方法:通过修改/etc/environment文件设置PATH环境变量 另一种设置全局PATH环境变量方法是修改/etc/environment文件。这个文件包含了系统的环境变量设置,可以在其添加PATH环境变量的设置,例如: $ sudo vi /etc/environment PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games" 在保存修改后,要使用新的PATH环境变量,需要重新启动系统或执行以下命令: $ source /etc/environment 总的来说,以上三种方法都可以设置PATH环境变量,但使用export命令只能在当前shell生效,而对于所有shell都生效的则应该使用其他方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值