linux 环境变量的配置

我发现有些人习惯在 /etc/profile 文件里面配置环境变量,在这里配置挺不好的。

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

通过以上文件可以看到其实也可以在 /etc/profile.d 目录下配置环境变量,这样更容易维护。

现在以 java 环境变量为例:

cd /etc/profile.d
sudo vi java.sh
###########################
JAVA_HOME=/opt/jdk1.8.0_271
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
###########################
sudo chmod 755 java.sh

最后重启一下就可以了。

如果安装 maven 的话:

sudo apt install maven
posted @ 2020-10-31 17:18  M-Anonymous  阅读(127)  评论(0编辑  收藏  举报