Linux env Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux env command and learn how to use it to manage environment variables and execute commands with custom environments. We will start by understanding the purpose and usage of the env command, then learn how to modify environment variables using it. Finally, we will practice executing commands with custom environment variables. The env command is a useful utility for running commands in a specific environment, which can be particularly helpful when working with different sets of environment variables.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/UserandGroupManagementGroup -.-> linux/env("`Environment Managing`") linux/UserandGroupManagementGroup -.-> linux/export("`Variable Exporting`") subgraph Lab Skills linux/echo -.-> lab-422663{{"`Linux env Command with Practical Examples`"}} linux/env -.-> lab-422663{{"`Linux env Command with Practical Examples`"}} linux/export -.-> lab-422663{{"`Linux env Command with Practical Examples`"}} end

Understand the Purpose and Usage of the env Command

In this step, we will explore the purpose and usage of the env command in Linux. The env command is a utility that allows you to run a command in a modified environment.

First, let's print the current environment variables using the env command:

env

Example output:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8
HOME=/home/labex
SHELL=/bin/bash

As you can see, the env command lists all the current environment variables and their values.

Next, let's create a new environment variable and run a command in that environment:

export MY_VAR="Hello, World!"
env MY_VAR="Hello, World!" echo $MY_VAR

Example output:

Hello, World!

In this example, we first set the MY_VAR environment variable using the export command. Then, we use the env command to run the echo command with the MY_VAR environment variable set to "Hello, World!".

The env command is useful when you need to run a command in a specific environment, for example, when you need to run a command with different environment variables than the current shell.

Modify Environment Variables Using the env Command

In this step, we will learn how to modify environment variables using the env command.

First, let's create a new environment variable:

export MY_VAR="Hello, World!"
echo $MY_VAR

Example output:

Hello, World!

Now, let's use the env command to modify the value of the MY_VAR environment variable:

env MY_VAR="Goodbye, World!" echo $MY_VAR

Example output:

Goodbye, World!

In this example, we used the env command to set the MY_VAR environment variable to "Goodbye, World!" and then echoed the value of MY_VAR.

You can also use the env command to remove an environment variable:

env -u MY_VAR echo $MY_VAR

Example output:

In this example, we used the -u option to unset the MY_VAR environment variable, and then echoed the value of MY_VAR, which is now empty.

The env command is a powerful tool for managing environment variables in your shell. You can use it to set, modify, and remove environment variables as needed for your specific use case.

Execute Commands with Custom Environment Variables

In this final step, we will learn how to execute commands with custom environment variables using the env command.

First, let's create a new environment variable and use it in a command:

export MY_MESSAGE="Hello from the custom environment!"
env MY_MESSAGE=$MY_MESSAGE echo $MY_MESSAGE

Example output:

Hello from the custom environment!

In this example, we first set the MY_MESSAGE environment variable using the export command. Then, we use the env command to run the echo command with the MY_MESSAGE environment variable set to its value.

You can also use the env command to run a command with multiple custom environment variables:

env MY_MESSAGE="Hello" MY_NAME="John" echo "$MY_MESSAGE, my name is $MY_NAME!"

Example output:

Hello, my name is John!

In this example, we set two custom environment variables, MY_MESSAGE and MY_NAME, and then use the env command to run the echo command with both variables.

The env command is particularly useful when you need to run a command in a specific environment, such as when you're working with different versions of software or libraries that require different environment configurations.

Summary

In this lab, we explored the purpose and usage of the env command in Linux. We learned how to print the current environment variables, create and modify new environment variables, and execute commands with custom environment variables. We also discovered how to remove an environment variable using the env command. These skills are useful when you need to run a command in a specific environment or manage environment variables in your shell.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like