我有一个 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
这是正确的吗?这很长吗?如果很长我怎样才能让它变短。请帮忙
答案1
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
消除重复项。
您可以pathfunc.tgz
从以下位置获取该文件 https://web.archive.org/web/20061210054813/http://www.netspinner.co.uk:80/Downloads/pathfunc.tgz
答案2
$ 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
变量包含路径的多个副本,使其过长。
这可以通过 shell 特定的(并且可能是唯一的)方式来处理zsh
。在该 shell 中,标量$PATH
变量与数组变量“绑定” $path
(在上面的一小段 shell 代码中使用),这意味着更改一个变量将自动更新另一个变量。此外,在zsh
shell 中,数组变量可以声明为“仅包含唯一元素”。将其应用于PATH
和path
变量,我们得到了一种方法来确保PATH
不会像您所做的那样增长。
typeset -g -U PATH path
该-g
选项使其typeset
将给定变量声明为“全局”(而不是当前范围的“局部”),-U
并使path
变量仅保留每个变量的第一次出现。
将该命令添加到或主目录中typeset
的第一个命令将确保它对于终端中的交互式 shell 会话处于活动状态。~/.zshenv
~/.zprofile
~/.zshrc
$ 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
,则只有新路径的第一次出现才会保留在变量中。