Mac OSX 每次打開shell執行maven命令都需要`source /etc/profile` 怎麼辦?

Mac OSX 每次打開shell執行maven命令都需要source /etc/profile 怎麼辦?

1.1 問題描述

今天遇到一個奇怪的現象,在我的新電腦Mac OSX 10.15.3 上安裝並配置完maven 環境變量後,每次打開shell, 想要使用maven 命令,都需要重新source /etc/profile 執行下。

爲了解決這個問題,搜索了一些解決方案,最終嘗試成功。

1.2 問題分析

如果我們在終端中輸入如下命令:

cat /etc/shells

執行成功後,我們可以看到有mac osx 支持多個shell 類型

# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

而且值得注意的是:

在今年蘋果公司發佈了macOS Catalina(10.15),macOS的默認終端從bash變成了zsh.

也就是說

  • bash 是一個shell 外殼程序,zsh 也是一個shell 外殼程序。
  • profilezshrc 都是一個文本配置文件
  • 如果是centos 7 或mac osx 10.15 以下版本,默認shell 環境是bash,系統環境變量的配置文件是 /etc/profile 文件
  • 如果是mac osx 10.15 以上版本,默認shell 環境是zsh , 那麼系統環境變量的配置文件默認是 /etc/zshrc文件

1.3 解決方案

明確了原因,問題就很好解決了,解決思路可以分爲三種:

  • 第一種: 修改shell 類型,將zsh 修改還原爲 bash
    -這種方式不推薦,因爲既然新版默認使用了這個zsh,必然是因爲zsh 比bash更優秀。
  • 第二種:將etc/profile 中配置的環境變量追加到 etc/zshrc 文件最後幾行。
  • 第三種:在etc/zshrc 系統環境變量配置文件中添加一條命令 source etc/profile 然後執行 source /etc/zshrc 這種方式就是不影響之前的用戶習慣,我覺得是最佳解決方案,原理如下:
    • 系統啓動時候先加載etc/zshrc 中配置的環境變量命令
    • 然後發現了source etc/profile 命令然後開始執行
    • source etc/profile 命令執行後這個文件裏面配置的環境變量生效

1.3.1 第一步:修改環境變量配置文件讀寫權限

  • Mac OSX 如果是一個新的電腦針對這兩個文件只有只讀文件權限,因此需要將這個文件授予寫的權限。

爲了給系統環境變量配置文件添加讀寫權限,我們需要輸入如下命令:

給etc/profile 文件添加權限

sudo chmod 777 /etc/profile

etc/zshrc 文件添加權限

sudo chmod 777 /etc/zshrc

以上命令什麼意思呢?

  • sudo 如果不指定用戶,默認是切換到root 用戶身份執行命令
  • chmod 是一個文件權限修改命令集
  • 777 是權限代號,表示授予文件讀,寫,執行的權限。
    下面一張圖完美解釋了777 的含義。
    在這裏插入圖片描述

1.3.2 第二步: /etc/profile 文件中添加環境變量

  • 打開etc/profile 文件需要輸入如下命令:
vi  /etc/profile
  • 然後按下ESC 鍵,再按下i 鍵,進入編輯插入模式。
  • 最後一行添加如下內容

/etc/profile 文件最終內容如下:

# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc
fi

# Config JDK or JRE
export JAVA_HOME=/Library/java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home
export PATH=.:$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/jre/lib/rt.jar:.

# Config Others
export MAVEN_HOME=/opt/app/maven/apache-maven-3.6.3
export PATH=$PATH:$MAVEN_HOME/bin

1.3.3 第三步:修改etc/zshrc文件內容

修改etc/zshrc 文件內容如下:

[[ -n ${key[Delete]} ]] && bindkey "${key[Delete]}" delete-char
[[ -n ${key[Home]} ]] && bindkey "${key[Home]}" beginning-of-line
[[ -n ${key[End]} ]] && bindkey "${key[End]}" end-of-line
[[ -n ${key[Up]} ]] && bindkey "${key[Up]}" up-line-or-search
[[ -n ${key[Down]} ]] && bindkey "${key[Down]}" down-line-or-search

# Default prompt
PS1="%n@%m %1~ %# "

# Useful support for interacting with Terminal.app or other terminal programs
[ -r "/etc/zshrc_$TERM_PROGRAM" ] && . "/etc/zshrc_$TERM_PROGRAM"

# config bash System environment variables
source /etc/profile

思路是 /etc/zshrc 命令執行的時候調用source /etc/profile 裏面的環境變量

1.3.3 第四步: 讓環境變量配置文件生效

系統環境變量配置文件一旦修改後如果不想重啓,想立即生效,則需要輸入如下命令。

source /etc/zshrc

值得注意的是,今後我們修改環境變量在etc/profile 文件中,修改完成後不再需要執行source /etc/profile命令,而是執行上面這個命令,因爲這個配置文件裏面調用了source /etc/profile 命令。

重新打開shell, 進行驗證,大功告成。

2. 參考資料

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