Practice Exercise: Understanding Environment Variables
Objective:
Explore environment variables in a Linux system and learn how to manage and utilize them effectively.
Task 1: Displaying Environment Variables
- Open a terminal window.
- Use the
env
command to display a list of all environment variables currently set on your system. - Identify common environment variables like
PATH
,HOME
,USER
, andSHELL
. Take note of their values.[intern@intern-a1t-inf-lnx1 ~]$ env SHELL=/bin/bash PWD=/home/intern LOGNAME=intern XDG_SESSION_TYPE=tty MOTD_SHOWN=pam
Task 2: Creating Custom Environment Variables
- Create a new environment variable named
MY_VARIABLE
and set it to your name or any custom value using theexport
command. - Use the
env
command again to verify that your new environment variableMY_VARIABLE
has been added to the list.[intern@intern-a1t-inf-lnx1 ~]$ export MY_VARIABLE=variable_value [intern@intern-a1t-inf-lnx1 ~]$ env | grep MY_VARIABLE MY_VARIABLE=variable_value
Task 3: Modifying Environment Variables
- Change the value of the
MY_VARIABLE
environment variable to something else using theexport
command. - Verify that the value of
MY_VARIABLE
has been updated using theenv
command. - Try modifying one of the common environment variables, such as
PATH
orPS1
, temporarily and observe the changes.[intern@intern-a1t-inf-lnx1 ~]$ export MY_VARIABLE=variable_value2 [intern@intern-a1t-inf-lnx1 ~]$ env | grep MY_VARIABLE MY_VARIABLE=variable_value2 [intern@intern-a1t-inf-lnx1 ~]$ export PATH=/home/projects [intern@intern-a1t-inf-lnx1 ~]$ env | grep PATH Command 'grep' is available in the following places * /bin/grep * /usr/bin/grep The command could not be located because '/usr/bin:/bin' is not included in the PATH environment variable. grep: command not found Command 'env' is available in the following places * /bin/env * /usr/bin/env The command could not be located because '/bin:/usr/bin' is not included in the PATH environment variable. env: command not found
- As you can see by changing the PATH value the command env and grep can't be located
- The easiest way I can think of to fix it is to simply logout and login again.
[intern@intern-a1t-inf-lnx1 ~]$ env | grep PATH PATH=/home/intern/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
- Of course this login and logout will also result in your MY_VARIABLE not persisting
- Verify that
MY_VARIABLE
has been removed from the list of environment variables.[intern@intern-a1t-inf-lnx1 ~]$ env | grep MY_VARIABLE
- You can remove a environment variable as well by simply using the command unset
[intern@intern-a1t-inf-lnx1 ~]$ export MY_VARIABLE=variable_value [intern@intern-a1t-inf-lnx1 ~]$ env | grep MY_VARIABLE MY_VARIABLE=variable_value [intern@intern-a1t-inf-lnx1 ~]$ unset MY_VARIABLE [intern@intern-a1t-inf-lnx1 ~]$ env | grep MY_VARIABLE
Task 4: Using Environment Variables
- Use the
echo
command to display the value of theHOME
environment variable.[intern@intern-a1t-inf-lnx1 ~]$ echo $HOME /home/intern
- Yes, to expand variables in linux you need the
$
- Create a simple shell script that utilizes environment variables to print a customized greeting message based on the
USER
environment variable.# You can use your preffered editor [intern@intern-a1t-inf-lnx1 ~]$ vi user.sh
- With the following content
#!/bin/bash echo "Hello $USER"
- Don't worry if you don't understand some part of the script yet as we will discuss it in the shell scripting chapter
- Execute the script and take note of the output
[intern@intern-a1t-inf-lnx1 ~]$ /bin/bash user.sh Hello intern
Conclusion:
In this lab exercise, you've gained practical experience with environment variables in a Linux environment. You've learned how to display, create, modify, and use environment variables both in the terminal and within shell scripts. Understanding environment variables is essential for configuring and customizing your Linux system.