What is environment variable?
As the name suggests, environment variables are are part of the environment in which a process executes. e.g. if we run a command ‘ls‘ on command prompt on a Linux terminal, the Terminal is the environment in which the ls is executed, and environment variables are part of the terminal.
PATH environment variable
PATH environment variable is essentially a list of directories in which a binary file is looked up into. e.g. ls is a binary executable. How will the system know the path in which this binary file is located? Answer lies in PATH variable. The binary files are looked up in all the directories specified in PATH environment variable. Let us understand it a bit more
user@server:~$ which ls
/bin/ls
user@server:~$ echo $PATH
/home/user/bin:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
In above example, we used which command to figure out where the ls is picked up from. It is executed from the location /bin/ls. How does the system find it? When we execute command ls, it is looked up in all the directories specified in PATH variable. We can print it like any other variable using echo command, As we can see that /bin is part of PATH variable, and hence ls is found and executed. As an exercise, remove /bin from PATH variable and then execute ls command.
“Include Path” environment variable
In our C-code, we include system header files. The compiler must have a way to find out where to pick up the header files from? Answer once again lies in environment variables. The default include paths are implementation specific to every compiler. However if we want to add any additional include path in compilation process, we can use C_INCLUDE_PATH environment variable. e.g
user@server:~$ export C_INCLUDE_PATH=/home/user/h;/home/user/program/h
If this variable is defined in the environment in which we are compiling a code, the header files included in the code to be compiled will also be looked up in directories specified by C_INCLUDE_PATH, if not found in compiler default include files.