Standard Shell Exceptions

Pathname of $0

When the shell searches for a command name in the PATH and finds a shell script, POSIX specifies that it shall pass the command name as argv[0] and in the child shell script, $0 shall be set from argv[0] (POSIX Standard, section, line 779-784).

However, for an LSB shell, the system may implement either this behavior or $0 may be set to an absolute pathname of the shell script. [1]

Sourcing non-executable files



For example, bash behaves in the following way:
$ cat ~/prefix/bin/mycommand
echo $0
$ PATH=~/prefix/bin mycommand
With a POSIX shell, the output would be "mycommand".

When bash is executing an executable rather than a shell script, it seems to behave in the POSIX way.