Hello! In this lesson, we will delve into the topic of environment variables in Bash. Environment variables are named values that can be shared across scripts or even across your entire computer. By the end of this lesson, you'll be able to list and use common built-in environment variables, create and modify your own, add custom paths to the PATH variable, and utilize environment variables within scripts.
Let's get started!
Environment variables are named values that can influence the way running processes behave on a computer. They are used to store system-wide values that can be accessed by any process running within the operating system. For example, they can be used to define the location of executable files, the home directory of users, and system-specific settings like language and locale information. The variables we have already seen have been local variables, meaning they can only be accessed within the current script. Environment variables are accessible from any script you run.
Let's list all the environment variables available in the CodeSignal IDE. This can be achieved using the printenv command. The printenv command outputs all environment variables currently set for your shell session. These variables include system-wide variables as well as any custom variables you may have set.
An example output is:
All of these variables are accessible from any script you run.
Bash comes with several built-in environment variables that are universally useful. Here are a few commonly used ones:
HOME: Represents the home directory of the current user.SHELL: Represents the path of the current shell.PWD: Represents the current working directory.
You can display the values of these variables using the echo command.
-
The
HOMEenvironment variable represents the home directory of the current user. In this case, it prints/root, indicating that the current user isroot, and their home directory is located at/root. -
The
SHELLenvironment variable is/bin/bashwhich is the file path to the current shell being used. This variable indicates thatbashwill be used when a new shell session is started. -
The value of the
PWDenvironment variable is/usercode/FILESYSTEM, indicating that the current working directory is .
The PATH variable is an essential environment variable in Unix-like operating systems that tells the shell which directories to search for executable files when a command is entered. When you run a command in the terminal, the shell looks through the directories listed in the PATH variable, in order, until it finds an executable file that matches the command name.
The PATH variable contains a colon (:) separated list of directory paths. For example:
The output is:
When any Bash command like echo, cat, or touch is run, the shell looks through the following directories for an executable file named echo, cat, or touch. Let's inspect this output:
/root/.nvm/versions/node/v18.12.1/bin: Directory containing Node.js executables./usr/local/sbin: Directory for local system administrator binaries./usr/local/bin: Directory for locally installed executables.
Creating and modifying environment variables is straightforward. You use the export command to set or change the value of an environment variable. For example:
export MY_VAR="Hello, Bash!": Creates a new environment variableMY_VARand sets its value to "Hello, Bash!".echo $MY_VAR: Prints the value ofMY_VAR.
With MY_VAR set as an environment variable, you can access MY_VAR from any script, not just the current one.
The PATH variable is an essential environment variable that tells the shell where to look for executable files. Suppose we want to create our own Bash command called greet. To do this, we need to add the file path to the greet executable to the PATH variable.
Let's first create a file called greet.sh located at /usercode/FILESYSTEM that simply prints Hello World.
/usercode/FILESYSTEM/greet.sh
Now let's update the PATH variable to also look for executable files in /usercode/FILESYSTEM, then call our greet.sh executable.
solution.sh
export PATH="$PATH:/usercode/FILESYSTEM"adds/usercode/FILESYSTEMto the existingPATHvariable.
Environment variables can be used within scripts to make them more dynamic and flexible. Let's create a simple script called greet.sh that uses an environment variable.
solution.sh
greet.sh
export USER="Cosmo"creates an environment variable namedUSERwith the valueCosmo. The$USERvariable is now accessible from any script, including thegreet.shscript.- The
greet.shcommand calls the executable. - The
greet.shscript searches for the environment variableUSERand prints "Hello Cosmo"
Great job! In this lesson, you learned how to:
- List all environment variables using the
printenvcommand. - Utilize common built-in environment variables such as
HOME,SHELL, andPWD. - Understand the
PATHvariable and how it directs the shell to executable files. - Create and modify environment variables with the
exportcommand. - Add new directories to the
PATHvariable. - Use environment variables dynamically within scripts.
These skills are crucial for creating flexible and dynamic bash scripts. With this solid understanding, you're now ready to dive into the practice exercises. By practicing, you'll reinforce what you've learned and gain confidence in using environment variables in your own scripts. Happy scripting!
