Understanding Shell Sessions
Any time you initiate interaction with a shell, you’re creating a shell session. Shell sessions can be classified in the following ways:
- Interactive shells: When you sit down at a computer and enter commands on the command-line, you’re working with an interactive shell.
- Non-interactive shells: When a shell session is invoked from within a shell script, you’re working with a non-interactive shell.
- Login shells: If you log into a Linux machine that’s running in text mode, without a graphical interface, you’re working with a login shell. You can also work with a login shell on a desktop machine by invoking a Ctrl-Alt-Function_Key sequence to switch away from the desktop interface to a text mode terminal. (You can use function keys F1 through F6 for this.) Or, you can invoke the
bash -l
command in the normal terminal emulator to open a childbash
session in login mode. The final way to initiate a login shell session is to log into a machine remotely via Secure Shell. Regardless of whether the remote machine is of a text-mode or GUI-mode variety, your remote session will be of the login shell type. - Non-login shells: Any time you open a terminal emulator on a desktop Linux machine, you’re working with a non-login shell.
So, what does all this mean? Well, the difference between interactive and non-interactive shells is fairly obvious, so I won’t talk more about that. But, I would like to point out two different ways to know whether you’re working with a login shell or a non-login shell.
The first way is to use the shopt
command, like so:
[donnie@fedora ~]$ shopt login_shell
login_shell off
[donnie@fedora ~]$
The shopt
command can be used to set various configuration options for a bash
session. Here though, I’m using it without any option switches to just view the login_shell
setting. You see here that the login_shell
setting is off
, which means that I’m in a non-login shell here on my Fedora workstation. On my text mode Fedora Server virtual machine, the shopt
output looks like this:
[donnie@fedora-server ~]$ shopt login_shell
login_shell on
[donnie@fedora-server ~]$
As you see, the login_shell
parameter is on
.
The second way to tell if you’re in a login shell is to use the echo $0
command, like this:
[donnie@fedora ~]$ echo $0
bash
[donnie@fedora ~]$
The $0
argument is what’s known as a positional parameter. I’ll provide in-depth coverage of positional parameters in Chapter 8, Basic Shell Script Construction, so don’t stress out about them just yet. All you need to know for now is that the echo $0
command shows the name of the script or executable that’s currently in use.
In this case, we’re in a bash
session, which means that the bash
executable is in use. But, how do we know whether or not we’re using a login shell? Well, it’s just that the bash
output is not preceded by a dash, which means that we’re not in a login shell. To show the difference, here’s what you’ll see on the text-mode Fedora Server virtual machine:
[donnie@fedora-server ~]$ echo $0
-bash
[donnie@fedora-server ~]$
The -bash
output indicates that I’m in a login shell.
Even from afar, I can read your mind. (Yes, I know that that’s creepy.) I know that you’re wondering why you need to know about these different types of shell sessions. Well, it’s just that there are several different bash
configuration files. The type of shell session you’re using determines which configuration files the session accesses. So, now that you know about the different types of shell sessions, we can look at these configuration files.