About
A login shell is an option of the shell that will executes:
- the login script (behaves as if the user were login)
- the logout script (at the end of the session)
Example
- Bash:
bash -l
Definition
A login shell is a shell:
- whose first character of argument zero ($0) is a -
- or started with the –login or -l option.
Command such as bash, sudo can create shells on the fly. In case, you want to run the login script, you need to run them as login shell with the –login or -l option.
Login and logout script
Login
When bash is invoked as:
- an interactive login shell,
- or as a non-interactive shell with the –login option,
it reads and executes commands from the following files in this order:
- /etc/profile,
- ~/.bash_profile, # only read by bash
- ~/.bash_login, # only read by bash
- and ~/.profile,
The file must exists and be readable.
The –noprofile option may be used when the shell is started to inhibit this behavior.
Logout
When a login shell exits, bash reads and executes commands from the files:
- /etc/logout
- ~/.bash_logout
- /etc/bash.bash_logout
- ~/logout
if the files exists.
Management
Test
In a script, you can test if the shell is a login shell with this predicate.
argv[0][0] == '-'