脚本:
#!/bin/zsh
IFS=$'\n' arr=($(alias | grep 'git' | grep "^[g|\'g]" | sed 's/=.*//' | sed "s/'//"))
echo $arr
这两行在普通命令行中工作:
~/test ····································································································································· test-Cj2sK_tx
❮ IFS=$'\n' arr=($(alias | grep 'git' | grep "^[g|\'g]" | sed 's/=.*//' | sed "s/'//"))
~/test ····································································································································· test-Cj2sK_tx
❯ echo $arr
g ga gaa gam gama gamc gams gamscp gap gapa gapt gau gav gb gbD gba gbd gbda gbl gbnm gbr gbs gbsb gbsg gbsr gbss gc gc!' gca gca!' gcam gcan!' gcans!' gcas gcasm gcb gcd gcf gcl gclean gcm gcmsg gcn!' gco gcount gcp gcpa gcpc gcs gcsm gd gdca gdct gdcw gds gdt gdw gf gfa gfg gfo gg gga ggpull ggpush ggsup ghh gignore gignored git-svn-dcommit-push gk gke gl glg glgg glgga glgm glgp glo glod glods glog gloga glol glola glols glp glum gm gma gmom gmt gmtvim gmum gp gpd gpf gpf!' gpoat gpristine gpsup gpu gpv gr gra grb grba grbc grbd grbi grbm grbo grbs grev grh grhh grm grmc grmv groh grrm grs grset grss grst grt gru grup grv gsb gsd gsh gsi gsps gsr gss gst gsta gstaa gstall gstc gstd gstl gstp gsts gsu gsw gswc gtl gts gtv gunignore gunwip gup gupa gupav gupv gwch gwip
但是,当这两个命令位于脚本文件中时,它不起作用......
~/test ····································································································································· test-Cj2sK_tx
❯ /bin/zsh ./d-alias.sh
# nothing here...
感谢您的任何帮助。非常感谢。
答案1
有一种更简单的方法可以做你想做的事:
print -r -- ${(k)aliases[(R)*git*]}
$aliases
是一个包含所有别名的特殊哈希表。 (看http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fparameter-Module)$...[(R)...]
做了一个撤销查找:它获取值(而不是键)与查询匹配的所有键值元组。 (看http://zsh.sourceforge.net/Doc/Release/Parameters.html#Subscript-Flags)${(k)...}
仅返回键(不是值)。 (看http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags)
但是,对于您的主要问题:当您这样做时/bin/zsh ./d-alias.sh
,它不起作用,因为您正在启动一个新的外壳程序来运行脚本。当您从另一个 shell 中启动一个新 shell 时,新 shell 会继承原始 shell 中导出的环境变量,但不会继承任何其他内容。此外,如果您启动一个新 shell 来运行脚本或命令(而不是简单地启动一个新 shell),则它不被视为交互式。这意味着您.zshrc
将不会获得来源,因此您的别名不会被初始化。
解决方案是将脚本作为函数加载:
% mkdir -p ~/Functions
% print 'print -r -- ${(k)aliases[(R)$~1]}' >> ~/Functions/d-alias
% fpath+=( ~/Functions )
% autoload -Uz d-alias
% d-alias '*git*'
g ga gaa gam gama gamc gams gamscp gap gapa gapt gau gav gb gbD gba gbd gbda gbl gbnm gbr gbs gbsb gbsg gbsr gbss gc gc!' gca gca!' gcam gcan!' gcans!' gcas gcasm gcb gcd gcf gcl gclean gcm gcmsg gcn!' gco gcount gcp gcpa gcpc gcs gcsm gd gdca gdct gdcw gds gdt gdw gf gfa gfg gfo gg gga ggpull ggpush ggsup ghh gignore gignored git-svn-dcommit-push gk gke gl glg glgg glgga glgm glgp glo glod glods glog gloga glol glola glols glp glum gm gma gmom gmt gmtvim gmum gp gpd gpf gpf!' gpoat gpristine gpsup gpu gpv gr gra grb grba grbc grbd grbi grbm grbo grbs grev grh grhh grm grmc grmv groh grrm grs grset grss grst grt gru grup grv gsb gsd gsh gsi gsps gsr gss gst gsta gstaa gstall gstc gstd gstl gstp gsts gsu gsw gswc gtl gts gtv gunignore gunwip gup gupa gupav gupv gwch gwip
答案2
您的别名在哪里定义?
如果您以 ./name.zsh 的形式运行脚本,它将运行 zsh 的新副本,而不是当前 shell 的分叉副本。 zsh 的新副本将来源您的 ~/.zshrc,但不会来源您的 ~/.zprofile 或 ~/.profile,因为它不是登录 shell。编辑:正如 Kusalananda 下面提到的,它不会为脚本提供 ~/.zshrc 来源,只有 ~/.zshenv 将为 zsh 脚本提供来源。
管道、循环、$() 和函数作为当前 shell 的分支运行,并保留所有别名和函数,因此 '$(alias)' 继承当前别名和函数,而 'zsh -c alias' 继承当前别名和函数不是。运行 shell 脚本也总是会为脚本创建一个新进程,就像“zsh -c alias”一样,而不是分叉当前 shell。
它应该通过将其引入当前 shell 来工作:
. ./name.zsh
如果您的别名在 ~/.zprofile 或 ~/.profile 中定义,则应将它们移至 ~/.zshrc。它源自 zsh 的每个副本,而不仅仅是登录 shell。