Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between a login shell and interactive shell? [closed]

What is a login shell and interactive shell, and what is a .bash_profile and .bashrc?

like image 234
caesar Avatar asked Aug 12 '13 12:08

caesar


People also ask

What is the difference between interactive and non-interactive shell?

An interactive shell reads commands from user input on a tty. Among other things, such a shell reads startup files on activation, displays a prompt, and enables job control by default. The user can interact with the shell. A shell running a script is always a non-interactive shell.

What is the difference between an interactive and a noninteractive shell which features of the shell have significance only in an interactive shell?

Simply put: Interactive shell require user input, while non-interactive shell are run by scripts and don't require user inputs.

What's a login shell?

A login shell is a shell given to a user upon login into their user account. This is initiated by using the -l or --login option, or placing a dash as the initial character of the command name, for example invoking bash as -bash. Sub shell.

How do you tell if a shell is a login shell?

If you get the result like “-bash” or “-su” means, you are on login shell. Make sure it has hyphen (-) as prefix. A Login Shell executes following scripts: Login shell executes /etc/profile.


1 Answers

An interactive shell is one started without non-option arguments, unless -s is specified, without specifying the -c option, and whose input and error output are both connected to terminals (as determined by isatty(3)), or one started with the -i option.

An interactive shell generally reads from and writes to a user’s terminal.

[gnu bash manual]

A login shell is a shell where you login. You can recognize a login shell from a ps -f listing, it will have a hyphen at the start of the program name, for example:

root      3561  3553  0 09:38 pts/0    00:00:00 -bash qa        7327  3432  0 10:46 pts/1    00:00:00 -bash 

An interactive shell is one which reads commands from it's standard-input, usually a terminal.

For example, if you login to bash using an xterm or terminal emulator like putty, then the session is both a login shell and an interactive one. If you then type bash then you enter an interactive shell, but it is not a login shell.

If a shell script (a file containing shell commands) is run, then it is neither a login shell nor an interactive one.

Start-up files are highly tailorable in bash:

When a login bash shell is invoked, then /etc/profile is sourced (executed in the current environment). After that, three files are checked for existence. The checks for these files are done in this order:

if /etc/profile exists, source (run) it
if ~/.bash_profile exists, source (run) it
if ~/.bash_login exists, source (run) it
if ~/.profile exists, source (run) it

Once a match is found, the other files are ignored, even if they exist. The /etc/bashrc file might be used by both the ~/.bash_profile and the ~/.bashrc files. That would mean that the /etc/bashrc file is sourced on all interactive invocations of bash, whether it is a login or non-login shell.

So, the .bashrc file is also run every time you request a new interactive shell. This does not include a shell script. Normally variables, aliases or functions are placed in this file.

Bash shell scripts read a different file if suitably instructed. If the user defines (usually in their own .bash_profile) a variable BASH_ENV which contains a filename, scripts will read this. If this variable is not set (and exported) then bash scripts will not read any startup files.

like image 157
cdarke Avatar answered Oct 05 '22 04:10

cdarke