Skip to content

Latest commit

 

History

History
50 lines (34 loc) · 1.98 KB

5.md

File metadata and controls

50 lines (34 loc) · 1.98 KB

Chapter 2: The Variable Verse 🌌

5 - Playing with environment variables

Understanding Environment Variables in Linux

What are Environment Variables in Linux?

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.

How to List Environment Variables in Linux

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

Using Environment Variables in Linux Shell Scripts

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"

Conclusion

Environment variables provide a flexible way to pass information to shell scripts and customize their behavior based on the system's configuration.

⬅️Previous-----------Next ➡️

Table of contents 🚀