Environment variables in Linux are dynamic named values that can affect the way running processes behave on a system. They contain information about the system environment, such as user preferences, system configuration, and program execution parameters. Environment variables are accessible by all processes running on the system and can influence their behavior.
To list environment variables in Linux, you can use the printenv
or env
command in the terminal. These commands display a list of all environment variables along with their values. Simply type printenv
or env
and press Enter to see the complete list of environment variables set on your system.
Example:
printenv
or
env
Environment variables can be utilized within shell scripts to customize their behavior or to provide configuration parameters. To access the value of an environment variable within a shell script, simply prepend the variable name with a dollar sign ($) as follows:
#!/bin/bash
# Accessing the value of an environment variable
echo "The value of HOME environment variable is: $HOME"
You can also set environment variables within a shell script using the export command followed by the variable name and value:
#!/bin/bash
# Setting an environment variable
export MY_VARIABLE="Hello, World!"
# Accessing the value of the newly set environment variable
echo "The value of MY_VARIABLE is: $MY_VARIABLE"
Environment variables provide a flexible way to pass information to shell scripts and customize their behavior based on the system's configuration.