Shell is an application with one end connected to the Linux kernel and the other end connected to the user. Shell is a bridge for communication between users and Linux systems. We all manage Linux systems through Shell.
We can use shell directly, or enter the username and password before using shell; the first type is called non-login type, and the second type is called login type.
We can enter commands one by one in the shell and view their output results in time. The whole process is constantly interacting with the shell, which is called interactive. We can also run a shell script file to allow all commands to be executed in batches and in one go, which is called non-interactive.
In short, there are four ways of running Shell:
- Interactive login Shell;
- Interactive non-login shell;
- Non-interactive login Shell;
- Non-interactive non-login shell.
Determine whether the shell is interactive
There are two simple ways to determine whether it is an interactive shell.
1) View the value of the variable -. If the value contains the letter i, it means interactive.
[Example 1] Output the value of - in the terminal that comes with the CentOS GNOME desktop environment:
[]$ echo $- himBH
It contains i, which is interactive.
[Example 2] Output the value of - in the shell script file:
[]$ cat #!/bin/bash echo $- []$ bash ./ hB
It does not contain i, it is non-interactive. Note that the Shell script must be run in a new process.
2) Check the value of variable PS1, if it is not empty, it is interactive, otherwise it is non-interactive, because the non-interactive will clear the variable.
[Example 1] Output the value of PS1 in the terminal provided by the CentOS GNOME desktop environment:
[mozhiyan@localhost]$ echo $PS1 [\u@\h \W]\$
Not empty, it is interactive.
[Example 2] Output the value of PS1 in the shell script file:
[]$ cat #!/bin/bash echo $PS1 []$ bash ./
Null value is non-interactive. Note that the Shell script must be run in a new process.
Determine whether the shell is logged in
It is also very simple to determine whether the shell is a login_shell. The value of on is expressed as a login and off is a non-login.
The shopt command is used to view or set behavior options in the shell that enhances the ease of use of the shell.
[Example 1] View the login_shell option under the terminal that comes with the CentOS GNOME desktop environment:
[]$ shopt login_shell login_shell off
[Example 2] Press the Ctrl+Alt+Fn key combination to switch to the virtual terminal, enter the user name and password to log in, and then check the login_shell option:
[]$ shopt login_shell login_shell on
[Example 3] View login_shel options in the shell script file:
[]$ cat #!/bin/bash shopt login_shell []$ bash ./ login_shell off
Simultaneously judge interactive and login
To determine whether it is interactive and login at the same time, you can simply use the following command:
echo $PS1; shopt login_shell
or
echo $-; shopt login_shell
Common Shell Startup Methods
1) When logging in to the shell through the Linux console (not the terminal that comes with the desktop environment) or ssh (this is the normal login method), it is an interactive login shell.
[]$ echo $PS1;shopt login_shell [\u@\h \W]\$ login_shell on
2) When executing the bash command, the default is non-login, and the --login option (abbreviated as -l) is added to the login form.
[]$ cat #!/bin/bash echo $-; shopt login_shell []$ bash -l ./ hB login_shell on
3) When using a group command or command surrounded by () to replace the child shell into it, the child shell will inherit the interaction and login properties of the parent shell. This seed process is just created fork again, does not execute extreme, and will not reload the configuration file
[]$ bash []$ (echo $PS1;shopt login_shell) [\u@\h \W]\$ login_shell off []$ bash -l []$ (echo $PS1;shopt login_shell) [\u@\h \W]\$ login_shell on
4) When ssh executes remote commands but does not log in, it is non-interactive and non-login.
[]$ ssh localhost 'echo $PS1;shopt login_shell' login_shell off
5) When opening a terminal in a Linux desktop environment, it is an interactive non-login shell.
Opening an interactive non-login shell in a desktop environment
This is the article about the implementation of four Shell operation methods (startup methods). For more related Shell operation methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!