PATH 變數的正確格式是什麼
我有一個 macOS,這是我做的時候得到的輸出
echo $PATH
usr/local/opt/avr-gcc@8/bin:/Users/hari/google-cloud-sdk/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/flutter/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/.npm/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/flutter/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/.pub-cache/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/flutter/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/.npm/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/flutter/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/Library/Android/sdk/platform-tools/.:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/flutter/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/.npm/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/flutter/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/.pub-cache/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/flutter/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/.npm/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/flutter/bin:/Users/hari/.edgedb/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/EdgeDB.framework/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/hari/.cargo/bin:/Users/hari/.fig/bin:/usr/local/go:/Users/hari/go:/Users/hari/go/bin:/Users/hari/.fzf/bin
這是正確的嗎?這很長嗎?如果很長,我怎麼能把它變短。請幫助
$ printf '%s\n' $path | sort | uniq -c 16 /Library/Apple/usr/bin 16 /Library/Frameworks/EdgeDB.framework/bin 16 /Library/Frameworks/Python.framework/Versions/3.9/bin 16 /Users/hari/.cargo/bin 16 /Users/hari/.edgedb/bin 16 /Users/hari/.fig/bin 1 /Users/hari/.fzf/bin 4 /Users/hari/.npm/bin 2 /Users/hari/.pub-cache/bin 1 /Users/hari/Library/Android/sdk/platform-tools/. 8 /Users/hari/flutter/bin 16 /Users/hari/go 16 /Users/hari/go/bin 1 /Users/hari/google-cloud-sdk/bin 16 /bin 16 /opt/X11/bin 16 /sbin 16 /usr/bin 16 /usr/local/bin 16 /usr/local/go 16 /usr/local/go/bin 16 /usr/sbin 1 usr/local/opt/avr-gcc@8/bin
如您所見,您的
PATH
變數包含多個路徑副本,使其過長。這可以以特定於(並且可能是唯一的)
zsh
shell 的方式處理。在該 shell 中,標量$PATH
變數與數組變數“綁定”$path
(在上面的一小段 shell 程式碼中使用),這意味著更改一個變數將自動更新另一個變數。此外,在zsh
shell 中,可以將數組變數聲明為“僅包含唯一元素”。將此應用於PATH
和path
變數,我們可以確保PATH
不會像您所做的那樣增長。typeset -g -U PATH path
使其將給定變數聲明為“全域”(而不是目前範圍的“局部”)的選項
-g
,並使變數僅保留每個變數的第一次出現。typeset``-U``path
將該命令添加
typeset
到第一個~/.zshenv
,~/.zprofile
或者~/.zshrc
存在於您的主目錄中的命令將確保它對於終端中的互動式 shell 會話處於活動狀態。$ typeset -g -U PATH path $ printf '%s\n' $path | sort | uniq -c 1 /Library/Apple/usr/bin 1 /Library/Frameworks/EdgeDB.framework/bin 1 /Library/Frameworks/Python.framework/Versions/3.9/bin 1 /Users/hari/.cargo/bin 1 /Users/hari/.edgedb/bin 1 /Users/hari/.fig/bin 1 /Users/hari/.fzf/bin 1 /Users/hari/.npm/bin 1 /Users/hari/.pub-cache/bin 1 /Users/hari/Library/Android/sdk/platform-tools/. 1 /Users/hari/flutter/bin 1 /Users/hari/go 1 /Users/hari/go/bin 1 /Users/hari/google-cloud-sdk/bin 1 /bin 1 /opt/X11/bin 1 /sbin 1 /usr/bin 1 /usr/local/bin 1 /usr/local/go 1 /usr/local/go/bin 1 /usr/sbin 1 usr/local/opt/avr-gcc@8/bin
要將路徑添加到末尾
PATH
,path+=( '/new/path' )
或者
path=( $path '/new/path' )
要將路徑添加到 的開頭
PATH
,path=( '/new/path' $path )
如果
path
變數已通過 設置為“唯一”typeset -U
,則只有第一次出現的新路徑將保留在變數中。
PATH
是您的 shell 搜尋執行檔的目錄的冒號分隔列表。PATH
操縱是困難的,但有工具。我使用 Stephen Collyer 的 bash_path_funcs,早在 2000 年就在 Linux Journal 中進行了描述:https://www.linuxjournal.com/article/3645 https://www.linuxjournal.com/article/3768 https://www.linuxjournal.com/article/3935
addpath
僅當路徑最初不存在時,該函式才會將條目添加到路徑中。delpath -n
從路徑中刪除所有不存在的目錄。uniqpath
消除重複。您可以從https://web.archive.org/web/20061210054813/http://www.netspinner.co.uk:80/Downloads/pathfunc.tgz
pathfunc.tgz
獲取文件