Shell Scripting – Interactive and Non-Interactive Shell
A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell accepts commands from the user and transforms them into a form that is understandable to the kernel. In other words, it acts as a mediator between ta user and the kernel unit of the operating system.
Some of the features of a shell are listed below:
- Wildcard substitution in file names (pattern-matching)
- Command history
- Filename substitution
This article focuses upon the interactive and non-interactive shell.
An interactive shell is defined as the shell that simply takes commands as input on tty from the user and acknowledges the output to the user. This shell also reads startup files that occurred during activation and displays a prompt. It also enables job control by default. It is also clear from the name, it is a shell with which we can interact. An interactive script is a script that requires input from the user. Interactive scripts couldn’t run in the background as they required input from the user. For an interactive shell, the prompt variable necessarily is set to ($PS1).
For example, a bash shell is an interactive shell.
How to start an interactive shell?
We can start an interactive shell by giving the name of the shell after we logged into the system.
This will start bash shell.
As the name implies, a non-interactive shell is a type of shell that doesn’t interact with the user. We can run it through a script or similar. Also, it can be run through some automated process. In this case .bashrc and .profile files do not get executed. The non-interactive shell influences the PATH variable. It is highly recommended to use the full path for a command in non-interactive shells. Non-interactive scripts can smoothly run in the background easily. This shell is generally a non-login shell because the calling user has logged in already. A shell that runs a script is always considered a non-interactive shell.
Scripts like Init and startup are considered non-interactive since they must run without human intervention.
How to check which type of shell is being used?
The type of shell being used can be detected (BASH only). We can determine if we are using an interactive or non-interactive shell by,
[[ $- == *i* ]] && echo ‘Interactive’ || echo ‘not-interactive’