7

man bash says,

 A login shell is one whose first character of argument zero is a -,  or
 one started with the --login option.

When we invoke a terminal such as mintty - on Cygwin, we can see a single hyphen (-) from $0.

$ echo $0
-bash

But if we invoke a login shell with --login option, we can not see -.

$bash --login
$echo $0
bash

My questions are:

  1. What is the role of -? Although we can not see it on login shell invoked --login, we can check shell's type by just querying related shell options like shopt -q login_shell. What is it for?

  2. If there is an important role of -, why we can not see it on login shell invoked with --login?

  3. How can we invoke a login shell manually whose zero argument starts with -?

  4. It is the case for other shells as such zsh, csh?

1

2 Answers 2

11

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. It tells the shell program that it should act as a login shell (read .profile, etc.). The shell program can choose to act as a login shell in other circumstances (for example, bash and zsh act as a login shell if passed the -l command line option).

To answer your questions:

  1. The role of - is to tell the shell to behave as a login shell. For bash, passing --login on the command line is equivalent.
  2. You can see the role of - on bash called with the --login option — it reads ~/.profile and so on. You can't see -bash if bash was invoked as bash --login rather than -bash because bash wasn't invoked as -bash.
  3. Typical shells don't let you choose the zeroth argument, it has to be the command name. With zsh, you can do ARGV0=-bash bash and with ksh93, bash or zsh, you can do (exec -a -bash bash). In mksh or zsh, you can do hash -- -bash=/bin/bash; -bash. You cannot do something like ln -s /bin/bash ./-bash; ./-bash as it's the whole argv[0] that has to start with -. You could do PATH=:$PATH; -bash, but then it has that nasty side effect of adding "" in front of $PATH.
  4. Yes.

See also Difference between Login Shell and Non-Login Shell?

2
  • If we invoke a shell with -c option like bash -c 'echo $0' -bash, then it will spawn a login shell too?
    – MS.Kim
    May 13, 2014 at 15:51
  • 1
    @MS.Kim, not anymore that running a script whose path starts with -. That sets the inline script's $0, not the bash process argv[0] to -bash. May 13, 2014 at 16:32
2

Question 1 and 2

As part from man bash that you give in your question, the man page said or. So you have two ways to start login shell:

  • first character of argument zero is a -.
  • started with the --login option.

and two way is independent from each other. A shell whose first character of argument zero is a - must be a login shell. But A shell whose first character of argument zero is not a - still can be a login shell or not. It is a login shell when invoked with --login.

Another way to check a login shell:

$ cuonglm at ~
$ bash --login
$ cuonglm at ~
$ grep -- -l /proc/$$/cmdline
Binary file /proc/7620/cmdline matches

Question 3

A simple way:

$ cuonglm at ~
$ sudo su -
% root at ~
% shopt -q login_shell && echo "Login shell" || echo "Not login shell"
Login shell

Question 4

AFAIK, zsh and csh is the same as bash in this case.

2
  • 1
    exec -a -bash bash will also spawn a login shell for question 3.
    – llua
    May 11, 2014 at 18:26
  • @llua, If we just set the first character of argument zero, will it spawn a login shell? For example, will bash -c bash' -bash also spawn a login shell? I tried this, but it seems not.
    – MS.Kim
    May 12, 2014 at 0:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .