2

The $0 shell parameter holds the name of the calling program. If you make a symbolic link named my_command in your home directory to the command /usr/local/bin/command1 and you execute it by typing ./my_command, what will be the value of $0? Can anyone also explain why? Thanks!

  • 3
    That's how busybox or lvm works... many commands in a single binary, called via symlink, and $0 tells the binary what it was called as. – frostschutz Oct 7 '15 at 20:23
5

$0 doesn't hold the name of the calling program but the name of the called program. The called program is ./my_command so $0 will be ./my_command too.

The fact it is a symbolic link doesn't make a difference.

| improve this answer | |
1

A command line (or rather, in UNIX, an array of command line arguments), including what would become $0, is passed to the exec call by the calling program.

Symlink resolution, shebang processing etc. is only done after that, by the system, as part of processing the exec call.

So, whatever the calling program (the shell in your case) specified as the first item in argv (/.my_command in your case) becomes $0.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.