Understanding the Shell Environment
When you are working from a shell, an environment is created to ensure that all that is happening is happening the right way. This environment consists of variables that define the user environment, such as the $PATH variable discussed earlier. In this section, you get a brief overview of the shell environment and how it is created.
Understanding Variables
The Linux shell environment consists of many variables. Variables are fixed names that can be assigned dynamic values. An example of a variable is $LANG, which in my shell is set to en_US.UTF-8. This value (which may differ on your system) ensures that I can work in the English language using settings that are common in the English language (think of how date and time are displayed).
The advantage of working with variables for scripts and programs is that the program only has to use the name of the variable without taking interest in the specific value that is assigned to the variable. Because different users have different needs, the variables that are set in a user environment will differ. To get an overview of the current variables defined in your shell environment, type the env command, which will show environment variables that are used to set important system settings. Example 2-1 shows some lines of the output of this command.
Example 2-1 Displaying the Current Environment
As you can see from Example 2-1, to define a variable, you type the name of the variable, followed by an equal sign (=) and the value that is assigned to the specific variable. To read the value of a variable, you can use the echo command (among others), followed by the name of the variable, as in echo $PATH, which reads the current value of the $PATH variable and prints that to STDOUT. For now, you do not have to know much more about variables. You can read about more advanced use of variables in Chapter 19, “An Introduction to Automation with Bash Shell Scripting.”
Recognizing Environment Configuration Files
When a user logs in, an environment is created for that user automatically. This happens based on the following four configuration files, where some script code can be specified and where variables can be defined:
/etc/profile: This is the generic file that is processed by all users upon login.
/etc/bashrc: This file is processed when subshells are started.
~/.bash_profile: In this file, user-specific login shell variables can be defined.
~/.bashrc: In this user-specific file, subshell variables can be defined.
As you have seen, in these files a distinction is made between a login shell and a subshell. A login shell is the first shell that is opened for a user after the user has logged in. From the login shell, a user may run scripts, which will start a subshell of that login shell. Bash allows for the creation of a different environment in the login shell and in the subshell, but to make sure the same settings are used in all shells, it’s a good idea to include subshell settings in the login shell as well.
Using /etc/motd and /etc/issue
To display messages during the login process, Bash uses the /etc/motd and the /etc/issue files. Messages in /etc/motd display after a user has successfully logged in to a shell. (Note that users in a graphical environment do not see its contents after a graphical login.) Using /etc/motd can be a convenient way for system administrators to inform users about an issue or a security policy, for example.
Another way to send information to users is by using /etc/issue. The contents of this file display before the user logs in from a text-based console interface. Using this file provides an excellent means of specifying login instructions to users who are not logged in yet.
In Exercise 2-6, you can practice the topics that have been discussed in this section.