Users:
There can be multiple users on an operating system. Each user has its own environment. Each user can configure its own environment/account by setting preferences.
These OS configurations should be isolated from other user environments. The operating system saves all these configurations in environment variables.
Environment Variables:
Environment variables are pieces of information defined by key=value pairs. Variables stores the information. Environment variables are available for the whole environment. By convention names are defined in UPPER CASE. Environment variables are variables, which means they can be changed. User can change these environment variables values.
List all Environment Variables:
To list all the environment variables printenv
is used.
Print Specific Environment Variable:
There are three commands to print specific environment variables.
printenv:
To print a specific environment variable using printenv,
printenv [VARIABLE-NAME]
is used.
grep:
To print the variables with same pattern
printenv | grep [pattern]
is used.
By Refrence:
To print a specific environment variable by reference
echo $[VARIABLE-NAME]
is used.$
is used for referencing.
Creating Environment Variables:
To create new environment variables export [VARIABLE-NAME]=[value]
is used.
Deleting Environment Variables:
To delete environment variables unset [VARIABLE-NAME]
is used.
Persisting Environment Variables:
Whenever you create environment variables on command they are available for that session only. Persisting environment variables typically involves setting them in a way that they are available every time a user logs in or a new shell session is started. Follow the steps to persist environment variables.
Open .bashrc file and add Environment Variables:
Go to the
/home
directory and open.bashrc
by usingnano .bashrc
. Create environment variable there.export [VARIABLE-NAME]=[value]
.
Load variables:
The
source
command is used to execute commands from a file within the current shell session. To load the new environment variables into the current shell sessionsource ~/.bashrc
is used.
Close and Open Terminal Again:
Close the terminal and open in again and then print the variable that you made.
These variables are now saved permanently.
Persisting Environment Variables (system wide):
~/.bashrc
is a user specific file. To set the environment variable for all the users in an operating system there is a file in /etc
directory. You can see the contents of this file by using cat /etc/environment
.
PATH Environment Variable:
List of directories to executable files, separated by :
Tells the shell which directories to search for the executable in response to our executed command.
Due to the PATH variable we can easily run the commands instead of giving the path before the commands. e.g. we don't need to write /usr/bin/ls
to list the contents in the directory, we simply write ls
.
Add a Custom Command:
If we create an executable program (shell script) and provide the PATH variable with the location of the executable program, we can run command to execute that program directly from anywhere in the file system like we run the ls
command. As PATH is the global variable any user in the operating system can run that command to execute that program. Follow the steps to create a custom command:
Create a simple script. If you don't know how to write a script click here.
-
Now give execute permission to all the users.
-
Open
.bashrc
file and writePATH=$PATH:[path-to-script]
and then save the file. -
now persist using
source ~/.bashrc
. -
If we go to another directory where the script is not saved and type there the name of script and press enter, the script will be executed.