Differentiate between interactive and non-interactive shells, logins, and non-logon shells

Source: Internet
Author: User
Tags echo command

The difference between the interactive shell and the non-interactive shell, the login shell, and the non-logon shell.

First, this is divided into two different dimensions, one is interactive, and the other is login.

Interactive shell and non-interactive shell (interactive shell and non-interactive shell)


The interactive mode is executed on the terminal, the shell waits for your input, and immediately executes the commands you submit. This pattern is called interactive because the shell interacts with the user. This pattern is also familiar to most users: Log in, execute commands, and exit. When you quit, the shell is terminated.

The shell can also run in a different mode: Non-interactive mode, which is executed in a shell script (non-interactive) way. In this mode, the shell does not interact with you, but instead reads the commands stored in the file and executes them. When it reads the end of the file, Eof,shell terminates.

You can distinguish between interactive and non-interactive shells by printing the value of the "$-" variable (which represents the option flag for the current shell) and viewing the "I" option (representing the interactive shell).

master@jay-intel:~/workspace/shell$ Echo $-
Himbh
master@jay-intel:~/workspace/shell$./test.sh
Echo $-
Hb


login Shell and non-logon shell

Login Shell: A shell that requires user name, password login to enter (or shell generated through the "–login" option).

Non-logon shell: Of course, there is no need to enter a username and password to open the shell, for example: direct command "Bash" is to open a new non-login shell, in GNOME or KDE open a "terminal" (terminal) window program is also a non-login shell.

Execute Exit command, exit a shell (login or non-login shell);

Execute the logout command and exit the login shell (you cannot exit the non-logon shell).

master@jay-intel:~$ Bash--login
master@jay-intel:~$ Logout
master@jay-intel:~$ Bash--login
master@jay-intel:~$ exit
Logout

master@jay-intel:~$ Bash
master@jay-intel:~$ Logout
Bash:logout:not login Shell:use ' exit '
master@jay-intel:~$ exit
Exit

When bash is the login shell, its process name is "-bash" rather than "bash". For example, the following command line demonstrates:
Man Bash:a the login shell is one whose the character of Argument Zero is A-, or one started with the–login option.

# in the login shell:
[perf@host_220-188 ~]$ Echo $
-bash
[perf@host_220-188 ~]$ Ps-ef | grep '-bash ' | Grep-v grep
Root 16823 16821 0 May06 pts/0 00:00:00-bash
Perf 21135 21134 0 May07 pts/1 00:00:00-bash

#在一个非登陆shell中:
jay@jay-linux:~$ echo $
/bin/bash
jay@jay-linux:~$ Ps-ef | grep '-bash ' | Grep-v grep
jay@jay-linux:~$

Summarize the discussions you've seen in the Chinaunix forum about login shell and non-login shell, interactive shell and non-interactive Shell:

Login Shell: When init3, Mingetty spawn out of the login prompt, you enter the account number and password entered the shell prompt, the process is to log into the shell;
Interactive Shell: Next you get a prompt like "root@host/>", what you type, what it explains, this is the interactive shell; you fork a shell (bash) in the current shell, This bash is also an interactive shell. Typically, the interactive shell is derived from the login shell.

Non Interactive Shell: You need to write a bash script that executes the Bash script.sh command in an external Shell, which (bash) executes from the first command to the last and then exits without any interaction with you, It is a non-interactive shell.

Interactive non-logon shell, for example, you open the "Terminal" window program in the GNOME graphical interface, compared to the login shell, it is "not logged in" and you do not need to enter a username and password, which is "interactive" compared to the non-interactive shell, as you said it is. What you enter, it explains what. "

For bash, the login shell, which includes the interactive logon shell and the –login shell that uses the option "", reads and executes the commands in the/etc/profile global configuration file first, and then finds ~/.bash_profile, ~/. Bash_login and ~/.profile These three configuration files, reading and executing commands in the first existing and readable file in these three. Unless prohibited by the "–noprofile" option.

In a non-login shell, only read ~/.BASHRC (and/ETC/BASH.BASHRC,/ETC/BASHRC) files, different distributions may be the same, such as the RHEL6.3 login shell only executed "~/.BASHRC" File (/ETC/BASHRC is not executed), and the/ETC/BASH.BASHRC and ~/.BASHRC files are executed sequentially in KUbuntu10.04.

For these rules, you can add an echo command directly to the appropriate configuration file to verify its authenticity.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.