2

I was reading about bash interactive and on interactive logins and found that running bash with -l option will start it as login shell but after doing so, when I ran command echo $0 to verify, it still shows as non login shell.

[root@localhost ~]# bash -l
[root@localhost ~]# echo $0
bash

It should show - as prefix to shell name. Please confirm if I am going wrong.

4

To have an - as the first character of the command name is just one way to signal a login shell, there are other signals. In fact, the correct way to detect that the present shell is a login shell is to ask the shell itself.

In the bash manual:

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

In bash:

shopt -p login_shell

Will print -u if the shell is not login, and -s if it is.

Lets test it:

$ ln -s $(which bash) ./-bash       # make a local copy of bash
$ PATH=$PATH:.                      # Big security problem, don't use it.
$ -bash                             # start the local copy of bash
$ echo $0                           # Its name start with an -
-bash                               
$ shopt -p login_shell              # Is it a login shell?
shopt -s login_shell                # Yes!, the answer is -s

$ exit                              # leave the login shell
$ PATH=${PATH%:.}                   # Remove the local pwd from the path.
$ rm ./-bash                        # Remove the local copy of bash

In fact, that $0 has a - doesn't have to mean that the shell is a login shell:

$  bash -c 'echo $0; shopt -p login_shell' -bash 1 2 3
-bash
shopt -u login_shell

The other way (from the manual) to get a login shell, is just ask for it:

$ bash --login                 # ask for a login shell
$ echo $0                      # What is its name?
bash
$ shopt -p login_shell         # Is it a login shell?
shopt -s login_shell           # Yes, it is!

$ exit                         # leave the login shell.

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.