${BASH_SOURCE[0]} 在 zsh 中等效? [英] ${BASH_SOURCE[0]} equivalent in zsh?
问题描述
标题应该说明一切.我正在 zsh 中寻找与 ${BASH_SOURCE[0]}
等效的代码.
The title should say it all. I'm looking for an equivalent to ${BASH_SOURCE[0]}
in zsh.
注意: 我一直在互联网上发现$0
等价于 ${BASH_SOURCE[0]}
",但这似乎为假:$0
似乎是执行命令的名称.(它是 argv[0]
,这是有道理的.)在我的脚本 (.zshrc
) 中回显 $0 为 $0<提供
zsh
/code>,这与 ${BASH_SOURCE[0]}
不同.事实上,${BASH_SOURCE[0]}
似乎在 zsh
中工作,除了在 .zshrc
文件中.
Note: I keep finding "$0
is equivalent to ${BASH_SOURCE[0]}
" around the Internet, but this seems to be false: $0
seems to be the name of the executing command. (It's argv[0]
, which makes sense.) Echoing $0 in my script (.zshrc
) gives zsh
for $0
, which isn't the same as what ${BASH_SOURCE[0]}
is. In fact, ${BASH_SOURCE[0]}
seems to work in zsh
, except for inside .zshrc
files.
我在 .zshrc
中真正在做什么(不起作用):
What I'm really doing in my .zshrc
(that isn't working):
echo ${BASH_SOURCE[0]}
source `dirname $0`/common-shell-rc.sh
源失败($0
是 zsh
)并且 echo
输出一个空行.
The source fails ($0
is zsh
) and the echo
outputs a blank line.
显然,要使 除非您设置 $0
工作,我需要选项 FUNCTION_ARGZERO
选项集.有什么方法可以测试这是否在脚本中设置?(以便我可以临时设置它)nofunction_argzero
,否则它显然已打开,并且它在我的外壳中已打开.$0
仍然一无所获.(我认为 b/c 我不在函数中.)
apparently, for It is apparently on unless you set $0
to work, I need the option FUNCTION_ARGZERO
option set. Any way to test if this is set in a script? (so that I can temporarily set it)nofunction_argzero
, and it is on in my shell. Still get nothing for $0
. (I think b/c I'm not in a function.)
推荐答案
${BASH_SOURCE[0]}
在 zsh 中等效为 ${(%):-%N}
code>,而不是 $0
(正如 OP 所说,后者在 .zshrc 中失败)
${BASH_SOURCE[0]}
equivalent in zsh is ${(%):-%N}
, NOT $0
(as OP said, the latter failed in .zshrc)
这里的%
表示对值进行提示扩展,%N
表示脚本的名称,源文件,或者 zsh 当前正在执行的 shell 函数,
Here %
indicates prompt expansion on the value,
%N
indicates "The name of the script, sourced file,
or shell function that zsh is currently executing,
以最近开始的为准.如果没有,这相当于参数 $0."(from man zshmisc
)
whichever was started most recently. If there is none, this is equivalent to the parameter $0."(from man zshmisc
)
这篇关于${BASH_SOURCE[0]} 在 zsh 中等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!