Environment Variables in Bash
Introduction to Environment Variables in Bash
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!
Listing Environment Variables
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.
Commonly Used Built-in Environment Variables
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/usercode/FILESYSTEM.
