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.

  • 1
    Possible duplicate of [Why we can not see a single hyphen from $0 on a login shell invoked with \`--login\` option?](https://unix.stackexchange.com/questions/128936/why-we-can-not-see-a-single-hyphen-from-0-on-a-login-shell-invoked-with-logi) – muru Sep 08 '19 at 15:18
  • 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. – happytomato Sep 08 '19 at 15:25
  • The mechanism is also covered elsewhere on this site: https://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 Sep 08 '19 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. –  Sep 08 '19 at 17:47
  • `bash` also has the `-l` and `--login` options to start a login shell in addition to parsing `argv[0]`. –  Sep 08 '19 at 17:49
  • Thanks for the comments. I found out that indeed login (among others) does expands the argv[0]. Just not in every situations. I tried in different environments and in some of them it works that way. – happytomato Sep 08 '19 at 18:06
  • You should post your research as an answer then. @happytomato **;-)** and then ping me and I'll come back and upvote! – Fabby Sep 13 '19 at 20:54
  • 1
    "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." https://unix.stackexchange.com/a/128983/9597 The above clearly explains the mechanism. This question should be closed as a duplicate. – Daniel Le Dec 04 '22 at 01:35

0 Answers0