The title should say it all. I'm looking for an equivalent to ${BASH_SOURCE[0]}
in zsh.
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.
What I'm really doing in my .zshrc
(that isn't working):
echo ${BASH_SOURCE[0]}
source `dirname $0`/common-shell-rc.sh
The source fails ($0
is zsh
) and the echo
outputs a blank line.
Edit: 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
equivalent is%x
, not%N
, as it refers to the enclosing file even when called inside of functions - in other words: use${(%):-%x}
– Gasconade