1

AFAIK at least the most popular shells use the argv[0] as an indicator whether they should behave as a login or a non-login shell. If argv[0] starts with a '-' the shell (bash and zsh at least) behaves as a login shell. Where is this dash appended to the original argument? Does the shell do it by itself figuring out whether it should be a login shell or not or is it done by login or init somehow?

Consider the following program (name it 'test').

int main (int argc, char *argv[]){
    printf("first argument: %s\n", argv[0]);
    return 0;
}

If this program is invoked from a shell it says 'First argument: test'. If some shell program e.g. zsh is invoked from a shell, the $0 variable is 'zsh'. However if users default login shell in passwd(5) is defined to zsh, then the $0 variable is '-zsh' after the first login. If I change the user default shell to test, the above program after login still prints 'test', not '-test'.

So, is the argv[0] set by some exec() call (from where) or does the shell set it by itself (based on what condition?).

This question differs from why-we-can-not-see-a-single-hyphen-from-0-on-a-login-shell-invoked-with-logi. It asks the role of dash character and how to change it manually. It does not ask where it is appended to argv[0] in normal use cases. However the most popular answer in the other topic claims login to be responsible of the change of the argv[0]. It still lacks the information about the mechanism. Why does it change zsh to -zsh but not test to -test.

7
  • 1
    @muru This is not a duplicate question. However the most popular answer in the other topic claims login to be responsible of the change of the argv[0]. It still lacks the information about the mechanism. Why does it change zsh to -zsh but not test to -test. Commented Sep 8, 2019 at 15:25
  • The mechanism is also covered elsewhere on this site: unix.stackexchange.com/q/250681/70524 (exec family of functions). That said, it prints -test for me (tested via login on a TTY, su and SSH on Arch Linux).
    – muru
    Commented Sep 8, 2019 at 15:58
  • 1
    That argv[0] will be set by whatever program started the shell, and the mechanism is preceding the first argument string with a - when calling execv*(2): execl("/bin/bash", "-bash", (void*)0). Notice that many programs can start a "login" shell, not just obvious ones like login or sshd (eg. xterm -ls). Notably, tmux will start login shells by default in its windows.
    – user313992
    Commented Sep 8, 2019 at 17:47
  • bash also has the -l and --login options to start a login shell in addition to parsing argv[0].
    – user313992
    Commented Sep 8, 2019 at 17:49
  • 2
    "The leading dash in the process name (in argv[0], to be precise) is placed there by the process that calls the shell — login or some equivalent." unix.stackexchange.com/a/128983/9597 The above clearly explains the mechanism. This question should be closed as a duplicate.
    – Daniel Le
    Commented Dec 4, 2022 at 1:35

0

Browse other questions tagged .