CentOS設定系統內容變數

來源:互聯網
上載者:User

1)第一次嘗試的天真做法:

以註冊golang環境變數為例:

cd /etc/profile.dls -rtltouch custom.sh # or any file name you like to create a new filels -rtlecho "export PATH=\${PATH}:/usr/local/go/bin" > custom.sh && chmod 755 custom.shless custom.sh. custom.shecho $PATHreboot


不建議直接寫入/etc/profile,而是在/etc/profile.d檔案夾下建立一個自己寫的shell指令碼,內容是:

export PATH=${PATH}:/usr/local/go/bin

系統啟動會自動執行這一指令碼


詳情見/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}...for i in /etc/profile.d/*.sh ; do    if [ -r "$i" ]; then        if [ "${-#*i}" != "$-" ]; then             . "$i"        else            . "$i" >/dev/null        fi    fidone


$- 代表當前shell的選項,See 1  2  3

echo "$-" 顯示的是 himBH (5個選項)

${-#*i} 中的*這裡是字串萬用字元,#這裡是字串操作符,${string#match_string}

Deletes shortest match of $match_string from front of $string. See 4

所以${-#*i} 是 mBH


但是,上述做法之後,在Terminal中echo $PATH中會出現兩次/usr/local/go/bin。雖然無傷大雅,但也挺奇怪的,難道這個指令碼被執行了兩次。

是的,被執行了兩次:

~/.bashrc  ->  /etc/bashrc 中也執行了一遍。


2)恰當的做法:

利用/etc/profile (/etc/bashrc)中定義的pathmunge 函數去除重複:

echo "pathmunge /usr/local/go/bin after" > custom.sh && chmod 755 custom.sh

5

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.