-->

Understanding The Interactive and Non-Interactive shell Process

If you start a bash shell without logging into a system (if you just type bash at a CLI prompt, for example), you start what’s called an interactive shell. The interactive shell doesn’t act like the login shell, but it still provides a CLI prompt for you to enter commands.

Understanding The Interactive and Non-Interactive shell Process
Understanding The Interactive and Non-Interactive shell Process


If bash is started as an interactive shell, it doesn’t process the /etc/profile file. Instead, it only checks for the .bashrc file in the user’s HOME directory.

On this Linux CentOS distribution, this file looks like this:

$ cat .bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
$

The .bashrc file does two things. First, it checks for a common bashrc file in the /etc directory. Second, it provides a place for the user to enter personal command aliases and private script functions.

Understanding the non-interactive shell process

The last type of shell is a non-interactive subshell. This is the shell where the system can start to execute a shell script. This is different in that there isn’t a CLI prompt to worry about. However, you may want to run specific startup commands each time you start a script on your system.

Tip
Scripts can be executed in different ways. Only some execution methods start a subshell. 

To accommodate that situation, the bash shell provides the BASH_ENV environment variable. When the shell starts a non-interactive subshell process, it checks this environment variable for the startup file name to execute. If one is present, the shell executes the file’s commands, which typically include variables set for the shell scripts.

On this CentOS Linux distribution, this environment value is not set by default. When a variable is not set, the printenv command simply returns the CLI prompt:

$ printenv BASH_ENV
$

On this Ubuntu distribution, the BASH_ENV variable isn’t set either. Remember that, when a variable is not set, the echo command displays a blank line and returns the CLI prompt:

$ echo $BASH_ENV
$

So if the BASH_ENV variable isn’t set, how do the shell scripts get their environment variables? Remember that some shell script execution methods start a subshell, also called a child shell. A child shell inherits its parent shell’s exported variables.

For example, if the parent shell was a login shell and had variables set and exported in the /etc/profile file, /etc/profile.d/*.sh files, and the $HOME/.bashrc file, the child shell for the script inherits these variables.

However, remember that any variables set but not exported by the parent shell are local variables. Local variables are not inherited by a subshell.

For scripts that do not start a subshell, the variables are already available in the current shell. Thus, even if BASH_ENV is not set, both the current shell’s local and global variables are present to be used.

0 Response to "Understanding The Interactive and Non-Interactive shell Process"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel