${BASH_SOURCE[0]} equivalent in zsh?
shell, zsh
Solution
`${BASH_SOURCE[0]}` equivalent in zsh is `${(%):-%N}`, NOT `$0`(as OP said, the latter failed in .zshrc)
Here `%` indicates prompt expansion on the value, `%N` indicates "The name of the script, sourced file, or shell function that zsh is currently executing,
whichever was started most recently. If there is none, this is equivalent to the parameter $0."(from `man zshmisc`)
Problem
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 `$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) It is apparently on unless you set `nofunction_argzero`, and it is on in my shell. Still get nothing for `$0`. (I think b/c I'm not in a function.)