Skip to content

Latest commit

 

History

History
106 lines (70 loc) · 4.86 KB

linux_environment_variables.md

File metadata and controls

106 lines (70 loc) · 4.86 KB

Environment variables

Environment variables defined

Global variables or environment variables are variables available for any process or application running in the same environment. Global variables are being transferred from parent process to child program. They are used to store system-wide settings and configuration information, such as the current user's preferences, system paths, and language settings. Environment variables are an essential part of the Unix and Linux operating systems and are used extensively by command-line utilities and scripts.

The env or printenv commands can be used to display environment variables.

The $PATH environment variable

When you want the system to execute a command, you almost never have to give the full path to that command. For example, we know that the ls command is actually an executable file, located in the /bin directory (check with which ls), yet we don't have to enter the command /bin/ls for the computer to list the content of the current directory.

The $PATH environment variable is a list of directories separated by colons (:) that the shell searches when you enter a command. When you enter a command in the shell, the shell looks for an executable file with that name in each directory listed in the $PATH variable, in order. If it finds an executable file with that name, it runs it.

System commands are normal programs that exist in compiled form (e.g. ls, mkdir etc... ).

myuser@hostname:~$ which ls
/bin/ls
myuser@hostname:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:.....

The above example shows that ls is actually an executable file located under /bin/ls. The /bin path is part of the PATH env var, thus we are able to type ls shortly.

The export command

The export command is used to set environment variables in the current shell session or to export variables to child processes.

When a variable is exported using the export command, it becomes available to any child process that is spawned by the current shell. This is useful when you need to pass environment variables to programs or scripts that you run.

For example, let's say you want to add a directory called mytools to your PATH environment variable so that you can run executables stored in that directory. You can do this by running the following command:

export PATH=$PATH:/home/myuser/mytools

This command adds the directory /home/myuser/mytools to the existing PATH environment variable, which is a colon-separated list of directories that the shell searches for executable files.

If you only set the PATH variable without exporting it, it will only be available in the current shell session and will not be inherited by child processes.

PATH=$PATH:/home/myuser/mytools

The source command

If you don't want to start a new process when executing a script or command, but to run it in the current shell process, you should source it.

The below example demonstrate the usage of the source command. We will use a bash variable called $, which contains the current process ID.

Create the below bash file under print_pid.sh:

# Note that the $$ gives the value of the $ variable. 
echo $$

Let's execute this script, once within a new bash process, then when the script is sourced:

myuser@hostname:~$ echo $$
44132
myuser@hostname:~$ bash print_pid.sh
50299
myuser@hostname:~$ source print_pid.sh
44132

Exercises

✏️ Create your own Linux "command"

Let's create a shell program and add it to your $PATH env var. Execute the following commands line by line:

  1. In your home dir, create a directory called scripts. This dir will be added to the PATH soon.
  2. Create bash script in a file called myscript (without any extension), with the following content:
#!/bin/bash
echo my script is running...
  1. Test your script by bash myscript
  2. Give it execute permissions
  3. Copy your script into ~/scripts
  4. Add ~/scripts to the PATH (don't override the existing content of PATH, take a look at the above example).
  5. Test your new "command" by just typing myscript.
  6. Try to use the myscript command in another new terminal session. Does it work? Why?

✏️ Elvis custom ls command

The PATH variable on elvis' machine looks like:

[elvis@station elvis]$ echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

elvis created a custom program called ls. The program is located in /home/elvis/custom directory.

  1. What is the command that elvis should execute such that his version of ls would be executed in the current terminal session only?
  2. What is the command that elvis should execute such that Ubuntu's version of ls would be executed in the current and child terminal sessions?