Skip to main content
Add attribution.
Source Link

Posting this since I don't have enough reputation to comment on the accepted answer from Gilles 'SO- stop being evil' (I know, I am supposed to avoid this, but this is actually something that will save people some time and confusion).

The zsh part of the answer does not work in general, because you have to setopt function_argzero before sourcing the script.

From man zshoptions:

   FUNCTION_ARGZERO <C> <Z>
          When executing a shell function or sourcing a script, set $0 temporarily  to  the
          name  of the function/script.  Note that toggling FUNCTION_ARGZERO from on to off
          (or off to on) does not change the current value of  $0.   Only  the  state  upon
          entry to the function or script has an effect.  Compare POSIX_ARGZERO.

Posting this since I don't have enough reputation to comment on the accepted answer (I know, I am supposed to avoid this, but this is actually something that will save people some time and confusion).

The zsh part of the answer does not work in general, because you have to setopt function_argzero before sourcing the script.

From man zshoptions:

   FUNCTION_ARGZERO <C> <Z>
          When executing a shell function or sourcing a script, set $0 temporarily  to  the
          name  of the function/script.  Note that toggling FUNCTION_ARGZERO from on to off
          (or off to on) does not change the current value of  $0.   Only  the  state  upon
          entry to the function or script has an effect.  Compare POSIX_ARGZERO.

Posting this since I don't have enough reputation to comment on the accepted answer from Gilles 'SO- stop being evil' (I know, I am supposed to avoid this, but this is actually something that will save people some time and confusion).

The zsh part of the answer does not work in general, because you have to setopt function_argzero before sourcing the script.

From man zshoptions:

   FUNCTION_ARGZERO <C> <Z>
          When executing a shell function or sourcing a script, set $0 temporarily  to  the
          name  of the function/script.  Note that toggling FUNCTION_ARGZERO from on to off
          (or off to on) does not change the current value of  $0.   Only  the  state  upon
          entry to the function or script has an effect.  Compare POSIX_ARGZERO.
Source Link

Posting this since I don't have enough reputation to comment on the accepted answer (I know, I am supposed to avoid this, but this is actually something that will save people some time and confusion).

The zsh part of the answer does not work in general, because you have to setopt function_argzero before sourcing the script.

From man zshoptions:

   FUNCTION_ARGZERO <C> <Z>
          When executing a shell function or sourcing a script, set $0 temporarily  to  the
          name  of the function/script.  Note that toggling FUNCTION_ARGZERO from on to off
          (or off to on) does not change the current value of  $0.   Only  the  state  upon
          entry to the function or script has an effect.  Compare POSIX_ARGZERO.