Why is there a minus sign in "$0" on Mac?
The minus sign is the way the system tells the shell that it's invoked as a login shell and it should source ~/.profile
(for Bourne-compatible shells). This is true on Linux, OSX and every other unix. A script would not be run in a login shell. For a script, $0
is the name of the script file (with or without the full path).
ADDED: The man page does explain (almost all) the different cases:
“If bash is invoked with a file of commands, $0 is set to the name of that file.” This covers scripts executed with
bash myscript
, as well as the indirect case where the script is executed directly and starts with#!/bin/bash
.“If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present.” With
-c
,$0
is set to whatever the caller explicitly indicates.“Otherwise, it is set to the file name used to invoke bash, as given by argument zero.” A login shell falls into this case: the shell is invoked with no arguments other than argument zero, so
$0
is set to argument zero. It islogin
,su
, or whatever program handled the login that chooses the arguments that it passed to the shell, and prepends a-
to argument zero to tell the shell that it's a login shell.
Perhaps some explanation of argument zero is in order. When a program is executed, ultimately, an execve
system call takes place. That system call takes three arguments:
a file name, which must designate an existing, executable file. The kernel loads this file and transfers execution to it.
an array of strings, called the arguments. Element zero in this array is by convention the same file name as above, or the just the file name without the full path if the location of the executable was determined by searching the
$PATH
environment variable. There are exceptions to this convention, such as login shells.another array of strings, called the environment.
When you call a program from the shell by typing myprogram foo bar
, the arguments to execve
are:
1. /usr/bin/myprogram
(assuming this is where the shell found myprogram
)
2. myprogram
, foo
, bar
3. for each exported shell variable, the variable name followed by an equal sign and the value.
There is no general way to find the name of the executable file that was passed to execve
from the running program. Under Linux, it's usually available as /proc/$$/exe
where $$
is the process ID. Every unix makes it available to ps
but the inner workings of ps
differ widely. The executable may be deleted or renamed while the program is running; in this case ps
might report obsolete information or no information.
From man bash
:
exec [-cl] [-a name] [command [arguments]]
If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. ...