Chapter 11: Environment Variables

In this chapter, we’ll cover: 
- What environment variables are and why they matter. 
- How to view and set environment variables. 
- Customizing your shell environment using configuration files like .bashrc.


Why Learn About the Environment?

Environment variables are key to configuring how your shell and programs behave. They control everything from your command prompt to the paths where programs look for files. Understanding them allows you to customize your system and automate tasks more effectively.


1. What Are Environment Variables?

Environment variables are named values that store information about the environment in which processes run. They can affect the behavior of programs and scripts.

Common Environment Variables

  • HOME: Your home directory.
  • PATH: A list of directories where the shell looks for commands.
  • USER: Your username.
  • SHELL: The path to your current shell.

2. Viewing Environment Variables

You can view environment variables using the following commands.

printenv: Print All or Specific Variables

  • Print all environment variables: $ printenv
  • Print a specific variable: $ printenv HOME

echo: Print a Specific Variable

Use echo with a $ prefix to print the value of a variable.

$ echo $HOME
/home/user

3. Setting Environment Variables

You can set environment variables temporarily or permanently.

Temporary Variables

Set a variable for the current session:

$ MY_VAR="Hello, Linux!"
$ echo $MY_VAR
Hello, Linux!

Exporting Variables

To make a variable available to child processes, use export:

$ export MY_VAR="Hello, Linux!"

Permanent Variables

To make a variable persistent across sessions, add it to your shell configuration file (e.g., .bashrc):

$ echo 'export MY_VAR="Hello, Linux!"' >> ~/.bashrc
$ source ~/.bashrc

4. The PATH Variable

The PATH variable is one of the most important environment variables. It tells the shell where to look for executable files.

Viewing the PATH

$ echo $PATH
/usr/local/bin:/usr/bin:/bin

Adding to the PATH

To add a directory to the PATH:

$ export PATH=$PATH:/new/directory

For permanent changes, add the line to your .bashrc:

$ echo 'export PATH=$PATH:/new/directory' >> ~/.bashrc
$ source ~/.bashrc

5. Shell Configuration Files

Shell configuration files allow you to customize your environment. The most common file is .bashrc.

.bashrc

This file is executed whenever you start a new shell. It’s a great place to set environment variables, aliases, and functions.

  • Editing .bashrc: $ nano ~/.bashrc
  • Reloading .bashrc: $ source ~/.bashrc

Other Configuration Files

  • .bash_profile: Executed for login shells.
  • .profile: Similar to .bash_profile, but not specific to Bash.

6. Customizing the Prompt

You can customize your shell prompt by modifying the PS1 variable.

Example: Adding a Timestamp

Add the following line to your .bashrc:

$ export PS1="\u@\h [\t] \w\$ "

This sets the prompt to:

$ user@debian [12:34:56] ~$

Common PS1 Codes

  • \u: Username.
  • \h: Hostname.
  • \t: Time (HH:MM:SS).
  • \w: Current working directory.

7. Environment Variables in Scripts

Environment variables are often used in scripts to store configuration values or pass information between processes.

Example Script

#!/bin/bash
echo "Hello, $USER!"
echo "Your home directory is $HOME."

Extra: Environment Variable Best Practices

Use .env files for managing environment variables:

$ source .env

Load environment variables with dotenv:

$ dotenv

Managing environment variables efficiently is key to configuring your shell and applications. Using .env or .envrc files to store variables allows for easier management and portability across projects. Tools like dotenv or direnv can automate the process of loading these variables when entering a project directory. When setting system-wide environment variables, caution is advised to avoid conflicts or unexpected issues. A well-organized environment ensures consistent behavior and fosters reusability across different contexts.

# Example usage:
echo "VAR_NAME=value" >> .env
source .env
dotenv
direnv allow

Practice Time!

Let’s put your new skills to the test: 
1. Use printenv to view all environment variables. 
2. Set a temporary environment variable and print its value. 
3. Add a directory to your PATH and test it by running a command from that directory. 
4. Customize your shell prompt using the PS1 variable.


That’s it for Chapter 11! You’ve now learned how to work with environment variables and customize your shell environment. In the next chapter, we’ll dive into the command-line environment—exploring shell prompts, aliases, and shell customization. Until then, experiment with environment variables to make your Linux experience more personalized.


Prev: Chapter 10 | Next: Chapter 12