From the course: Cert Prep LPIC-1 Exam 102 (Version 5.0)

Set environment variables - Linux Tutorial

From the course: Cert Prep LPIC-1 Exam 102 (Version 5.0)

Start my 1-month free trial

Set environment variables

- [Instructor] There are two types of variables accessible in a shell session. Environmental variables are variables that are defined for the current shell and are inherited by any child processes or child shells. Shell variables are variables that are contained exclusively within the shell in which they were defined. They are often used to keep track of data like the current working directory. To see the environmental variables in a terminal type in print end and hit enter. If you want to get the value of an individual variable you can specify it. Type in printenv. And for instance, type in capital SHELL and hit enter. Another way is to use the echo command to print the value by typing in echo space dollar sign capital s-h-e-l-l, and hit enter. To see the shell variables we use the set command. Type in set and hit enter. By default the set command shows shell functions as well as variables which can be quite long. To change the behavior of set to posix mode which doesn't show functions use the dash o posix option. Type in set space dash o space posix and hit enter. Now type in set again and let's pipe it to less. Here we can see all the shell variables without the shell functions. To show the difference between environmental variables and shell variables, let's set a variable in our shell. Press Q to quit and then type in capital VAR equals capital TEST and hit enter. Now let's grep the output of set to view it. Type in set space pipe space grep space capital V-A-R and hit enter. And there it is. Now let's grep the output of printinf. Type in printenv space pipe space grep space capital VAR and hit enter and we can see that our variable is not a part of the environment. To turn our shell variable into an environmental variable we can just export it. Let's export the variable we just set. Type in export space capital V-A-R and hit enter. Verify with printenv. Type in printenv space pipe space grep space capital V-A-R and hit enter. And we can see that our variable's now a part of the environment. It isn't persistent since we didn't save it in a file. If we rebooted it would disappear. To unset environmental variables we can use the export command again. Type in export space dash n space capital V-A-R and hit enter. We can verify this by typing in printenv space pipe space grep space capital V-A-R and hit enter. We can see that the variable is no longer part of the environment. To unset shell variables we can use the unset command. Type in unset space capital V-A-R and hit enter. Earlier we changed a shell option by turning on posix mode by typing in set space dash O space posix. We can view all shell options by typing in set space dash o and hit enter. To unset any shell option use plus O. For instance, to unset posix mode type in set space plus o space posix and hit enter. Then we can verify this again by typing in set space dash o. We can change the value of environment and shell variables by setting them. We can also change batch options using the set command. However, these values only survive for the current login session. To make them persistent, we need to add them to one of the bash startup files. Bash stores its configuration settings in multiple startup files. Different files are processed depending on how the shell starts. When we log into Linux by typing in a password, Linux first executes the /etc/profile which in turn executes scripts in /etc/profile.d. These two items are the system-wide file and directories holding our environment and shell variables. Then bash executes the ._profile in the user's home directory which holds environment and shell variables specific to the user. The local .bash_profile in turn executes the local .bashrc file which holds aliases and bash functions. In CentOS the local .bashrc also executes the system wide /etc/bashrc file which holds system wide aliases and functions. When that is complete it returns control back to the local .bashrc file in the user's home directory. The local .bashrc file holds aliases and functions specific to the user. Once all of this is done the user logs in. If the user is already logged in and executes a shell script, it's executed in a shell, but it's not a login shell. Also, if a user's logged into the GUI and starts a shell, it's not a login shell. Generally speaking a login shell requires entering a password or providing a login key. For non-login shells, only the user's .bashrc gets run. Again, on CentOS the local .bashrc file executes the global /etc/bashrc file which holds system wide aliases and functions. Then it returns control back to the local .bashrc file again and finishes processing it. This file should contain aliases and functions specific to the user. Once this is all done, the shell starts. In summary the various bash profile files are for environmental and shell variables useful for interactive logins. If you want to set a variable for all users, set it in either the /etc/profile or a script in /etc/profile.d. If you want to set a variable specific to a user, change it in the user's .bash_profile. If you want to add aliases or functions for all users, add them to the /etc/bashrc. If you want to add aliases or functions specific to a user, add them to the user's .bashrc. It's important to note that when you add aliases or functions to the user's .bashrc file be sure to add them to the end of the file after .bashrc has been called to override system wide aliases and functions.

Contents