[[ $_ = $0 ]] && {
echo 'the script is being sourced.'
return 0
} It works.
❯ source traditional-way.zsh
the script is being sourced.
❯ ./traditional-way.zsh
probably, it was running as a subshell.However, this way is broken easily as follows.
:
[[ $_ = $0 ]] && {
echo 'the script is being sourced.'
return 0
}if [[ $_ = $0 ]]; then
echo 'the script is being sourced.'
return 0
fi❯ source traditional-way-wrong-1.zsh
probably, it was running as a subshell.
❯ source traditional-way-wrong-2.zsh
probably, it was running as a subshell.if [[ ${#funcstack[@]} -ne 0 ]]; then
echo 'the script is being sourced.'
return 0
fi❯ source effective-way.zsh
the script is being sourced.
❯ ./effective-way.zsh
certainly, it was running as a subshell.