Linux Environment Managing

LinuxBeginner
Practice Now

Introduction

Linux environment variables are key-value pairs that can affect how processes run on your system. Understanding how to manage these variables is crucial for Linux users, from beginners to advanced administrators.

In this lab, you will learn how to view existing environment variables, create your own temporary variables, modify important system variables like PATH, and make your changes persistent across login sessions. These skills form the foundation of Linux environment management and will help you customize your Linux experience and troubleshoot environment-related issues.

By the end of this lab, you will have practical experience with the commands and techniques needed to effectively manage environment variables in Linux.

Viewing Environment Variables

Environment variables store information that can be used by the system and applications. In this step, you will learn how to view existing environment variables on your Linux system.

First, let's create a dedicated directory for our lab exercises:

mkdir -p ~/project/env_lab

Navigate to this directory:

cd ~/project/env_lab

Now, to view all currently set environment variables, use the env command:

env

This command displays a list of all environment variables in your current session. The output will look similar to this:

PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
HOME=/home/labex
USER=labex
SHELL=/bin/zsh
...

Each line shows a variable name, followed by an equals sign (=), followed by its value. These variables define various aspects of your environment.

To view a specific environment variable, you can use the echo command with the variable name prefixed by a dollar sign ($). For example, to view the value of your home directory variable:

echo $HOME

This should display something like:

/home/labex

Similarly, to view your current user:

echo $USER

Expected output:

labex

Setting Temporary Environment Variables

Environment variables can be set temporarily for the current shell session. This is useful when you need a variable for a specific task without making permanent changes to your system.

To set a temporary environment variable, use the export command followed by the variable name and value:

export TEST_VAR="Hello, Linux"

This creates a variable named TEST_VAR with the value "Hello, Linux". To verify that the variable was set correctly, use echo:

echo $TEST_VAR

You should see:

Hello, Linux

You can also set multiple variables at once:

export VAR1="First" VAR2="Second"

Verify both variables:

echo $VAR1
echo $VAR2

Expected output:

First
Second

To see your new variables among all environment variables, run:

env | grep VAR

This will filter the output to show only lines containing "VAR", such as:

TEST_VAR=Hello, Linux
VAR1=First
VAR2=Second

Remember that variables created this way only exist in the current shell session. If you open a new terminal window or tab, these variables will not be available there.

Understanding and Modifying the PATH Variable

The PATH environment variable is one of the most important variables in Linux. It tells the system where to look for executable files when you type a command.

First, examine your current PATH:

echo $PATH

You'll see a colon-separated list of directories, similar to:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

When you type a command, the system searches these directories in order until it finds a matching executable.

Now, let's create a new directory for our custom scripts and add it to the PATH:

mkdir -p ~/project/env_lab/bin

To add this directory to the beginning of your PATH, use:

export PATH=~/project/env_lab/bin:$PATH

This prepends our new directory to the existing PATH. Verify the change:

echo $PATH

Now your output should start with your new directory:

/home/labex/project/env_lab/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Let's create a simple script to test our modified PATH:

cd ~/project/env_lab/bin

Create a file named hello using the nano editor:

nano hello

Add the following content to the file:

#!/bin/bash
echo "Hello from my custom script!"

Save the file by pressing Ctrl+O, then Enter, and exit with Ctrl+X.

Make the script executable:

chmod +x hello

Now, from any directory, you should be able to run your script just by typing its name:

cd ~/project/env_lab
hello

You should see:

Hello from my custom script!

This demonstrates how the PATH variable helps the system find executables, and how you can customize it to include your own script directories.

Using Environment Variables in Commands and Scripts

Environment variables can be used in various ways within commands and scripts. In this step, you'll learn how to utilize environment variables effectively.

First, let's create a new environment variable with your name:

export MY_NAME="Linux User"

You can use this variable in commands by prefixing it with $:

echo "Hello, $MY_NAME"

This should output:

Hello, Linux User

Environment variables can also be used with other commands. For example, let's create a directory named after our variable:

mkdir -p ~/project/env_lab/$MY_NAME

Verify the directory was created:

ls -la ~/project/env_lab/

You should see a directory named "Linux User" in the output.

Now, let's create a simple script that uses environment variables:

cd ~/project/env_lab
nano greet.sh

Add the following content:

#!/bin/bash

## Use existing environment variables
echo "Hello, $MY_NAME"
echo "Your home directory is $HOME"

## Use a variable passed to the script
echo "The current time and date is $(date)"

## Create a local variable
local_var="This is a local variable"
echo $local_var

## Export a new variable from the script
export GREETING="Have a nice day!"
echo $GREETING

Save and exit with Ctrl+O, Enter, and Ctrl+X.

Make the script executable:

chmod +x greet.sh

Run the script:

./greet.sh

You'll see output similar to:

Hello, Linux User
Your home directory is /home/labex
The current time and date is Fri Aug 18 12:34:56 UTC 2023
This is a local variable
Have a nice day!

Making Environment Variables Persistent

So far, all the environment variables we've set will be lost when you close your terminal session. In this step, you'll learn how to make environment variables persistent across sessions.

In Linux, shell startup files like .bashrc, .zshrc, or .profile are used to set persistent environment variables. Since our environment uses zsh, we'll use the .zshrc file.

First, let's open the .zshrc file:

nano ~/.zshrc

Navigate to the end of the file (use Ctrl+End or arrow keys) and add the following lines:

## Custom environment variables
export PERSIST_VAR="This variable will persist"
export PATH=$HOME/project/env_lab/bin:$PATH

Save and exit with Ctrl+O, Enter, and Ctrl+X.

To apply these changes without closing and reopening your terminal, source the file:

source ~/.zshrc

Now verify that your new variable is set:

echo $PERSIST_VAR

You should see:

This variable will persist

Now, even if you close and reopen your terminal, this variable will still be available.

To demonstrate, let's simulate closing and reopening the terminal by starting a new shell session:

zsh

Check if your variable is still available:

echo $PERSIST_VAR

You should still see:

This variable will persist

Exit this shell to return to your previous session:

exit

This method of adding environment variables to your shell's configuration file ensures they are available every time you log in to your system.

For system-wide environment variables that should be available to all users, administrators can add them to files in /etc/profile.d/ or edit /etc/environment.

Summary

In this lab, you learned the fundamentals of managing environment variables in Linux, which are essential for customizing your Linux experience and controlling how applications behave.

Key concepts covered include:

  • Viewing environment variables with the env command and displaying specific variables with echo $VARIABLE_NAME
  • Setting temporary environment variables using the export command
  • Understanding and modifying the PATH variable to control where Linux looks for executable programs
  • Using environment variables in commands and scripts to make your code more flexible
  • Making environment variables persistent by adding them to shell configuration files

These skills form the foundation of Linux environment management and are applicable across various Linux distributions. Understanding environment variables is crucial for Linux users at all levels, from beginners to advanced administrators and developers.

Environment variables help you customize your shell experience, control application behavior, and build more flexible scripts. As you continue your Linux journey, you'll find that proper environment management becomes increasingly important for more complex tasks like software development, system administration, and troubleshooting.