LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Difference between normal shell and login shell (https://www.linuxquestions.org/questions/linux-general-1/difference-between-normal-shell-and-login-shell-14983/)

Manish 02-24-2002 05:10 AM

Difference between normal shell and login shell
 
What is the difference between running a shell (e.g. bash) as a normal shell VS as a login shell (with the option -login, or prefixing hyphen to its sym. link) ??

Thanks.

trickykid 03-09-2002 02:17 PM

I don't understand your question. I thought a shell was a shell, either using bash, sh, csh, tcsh... etc etc... logging in or already logged in..

-trickykid

jeremy 03-09-2002 03:25 PM

You can invoke bash as a login shell or as an interactive shell. From the man page:

Quote:

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

An interactive shell is one whose standard input and output are both connected to terminals (as determined by
isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a
shell script or a startup file to test this state.

Login shells:
On login (subject to the -noprofile option):
if /etc/profile exists, source it.

if ~/.bash_profile exists, source it,
else if ~/.bash_login exists, source it,
else if ~/.profile exists, source it.

On exit:
if ~/.bash_logout exists, source it.

Non-login interactive shells:
On startup (subject to the -norc and -rcfile options):
if ~/.bashrc exists, source it.

Non-interactive shells:
On startup:
if the environment variable ENV is non-null, expand
it and source the file it names, as if the command
if [ "$ENV" ]; then . $ENV; fi
had been executed, but do not use PATH to search
for the pathname. When not started in Posix mode, bash
looks for BASH_ENV before ENV.

If Bash is invoked as sh, it tries to mimic the behavior of sh as closely as possible. For a login shell, it
attempts to source only /etc/profile and ~/.profile, in that order. The -noprofile option may still be used to
disable this behavior. A shell invoked as sh does not attempt to source any other startup files.
--jeremy

Manish 03-09-2002 10:02 PM

Thanks jeremy. :)

OsamaBinLogin 11-13-2012 06:58 PM

I didn't understand the docs either
 
Login shell is the first one you run when you 'log in': when you sign in on a SSH session, or add a new terminal emulator window or tab. To check:
$ echo $0
-bash
$ ps
456 ttys006 0:00.22 -bash
459 ttys007 0:03.81 bash
1633 ttys008 0:00.03 bash
10935 ttys009 0:00.07 -bash

-bash = login shell, bash = not login. processes 456 and 10935 are login shells.


Interactive shell is when you type in 'bash' from your login or another interactive shell. and stdin and stdout are terminals.To see if yours are terminals (they probably are if you're typing in), run these:
$ [ -t 0 ] && echo stdin is a tty
$ [ -t 1 ] && echo stdout is a tty

The $- env var tells you what options your shell runs in. i=interactive. for example:
$ echo $-
himBH

that one had stdin=terminal, this one has a pipe as stdin:
$ echo 'echo $-' | bash
hB


Login shell runs .profile (or .bash_profile or .bash_login) on startup. Set your env variables in there, cuz sub-shells will inherit the env vars. Then make .profile run your .bashrc:
. .bashrc

Interactive runs .bashrc . Since it inherits envs, but not aliases, put your aliases in this file.

Shell scripts, cron jobs, and the like, run with a bare shell: no bashrc or profiles are run. No kidding you get 4 env vars set, the bare minimum. Therefore your PATH won't work very well so you often have to give an abs pathname like in crontab:
right: 0 0 * * 2 /usr/sbin/apachectl restart
wrong: 0 0 * * 2 apachectl restart

linosaurusroot 11-14-2012 08:11 AM

> whose first character of argument zero is a -

That will make most sense to C programmers who know the variable argv[] as given to the main() function.

sundialsvcs 11-14-2012 08:29 AM

This option is used to tell Bash how it's supposed to behave. When you log in successfully, getty execs the specified shell-program to give you the command prompt. This shell instance (which replaces "getty") will be the root of all other processes in your session, and as such it needs to look and behave a little differently.

linosaurusroot 11-14-2012 08:30 AM

Quote:

Originally Posted by sundialsvcs (Post 4829194)
This option is used to tell Bash how it's supposed to behave. When you log in successfully, getty execs the specified shell-program to give you the command prompt. This shell instance (which replaces "getty") will be the root of all other processes in your session, and as such it needs to look and behave a little differently.

getty to login to -shell


All times are GMT -5. The time now is 05:03 PM.