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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章