env command in Linux with Examples
Last Updated :
03 Sep, 2024
The 'env' command in Linux is a versatile tool used to manage environment variables. It can print all the current environment variables, set new ones, or run a command within a custom environment. Additionally, 'env' is commonly used in shell scripts to launch the correct interpreter, ensuring the script runs with the intended environment settings.
Here, we will learn more about 'env' command in Linux along with some practical examples.
What Does the env Command Do?
The primary function of the 'env' command is to display environment variables or run commands within a modified environment. Environment variables are dynamic values that affect the processes or programs running on a system, such as 'PATH', 'HOME', and 'USER'. By using 'env', you can manipulate these variables to customize the behavior of your commands and scripts.
Syntax:
env [OPTION]... [-][NAME=VALUE]... [COMMAND [ARG]...]
- OPTIONS: Flags to modify the behavior of the 'env' command.
- NAME=VALUE: Defines environment variables and their values.
- COMMAND [ARG]: Specifies the command to be executed with the modified environment.
Options of the 'env' command
Here are some common ways to use the 'env' command along with practical examples:
1. Without any argument
Print out a list of all environment variables.
EXAMPLE:

2. '-i' or '--ignore-environment' or 'only -'
Runs a command with an empty environment
SYNTAX:
$ env -i your_command
Note: This completely clear out the environment, but it does not prevent your_command setting new variables.
EXAMPLE:
To clear the environment (creating a new environment without any existing environment variables) for a new shell

In this, it clears all the environment variable and then new shell sets the environment variable PWD. Thus in this new shell when we execute env, we only see one environment variable PWD.
3. '-u' or '--unset'
Remove variable from the environment
SYNTAX:
$ env -u variable_name
EXAMPLE:
Removing XDG_VTNR environment variable which you can see in the image above in the output of just 'env'.

4. '-0' or '--null'
End each output line with NULL, not newline
SYNTAX:
$ env -0
EXAMPLE:

5. '--version'
Display version information and exit.
SYNTAX:
$ env --version
EXAMPLE:

6. '--help'
Display a help message and exit.
SYNTAX:
$ env --help
EXAMPLE:

Conclusion
The 'env' command in Linux is an essential tool for managing environment variables and controlling the behavior of commands and scripts. Mastering 'env' can significantly enhance your efficiency and control over your Linux environment. By familiarizing yourself with its options and practical uses, you can leverage 'env' to customize your workflow and ensure your scripts and commands run as intended.
Similar Reads
dos2unix and unix2dos commands Sometimes, you will need to move files between windows and unix systems. Window files use the same format as Dos, where the end of line is signified by two characters, Carriage Return or CR or \r followed by Line Feed or LF or \n. Unix files, on the other hand, use only Line Feed (\n). unix2dos is a
4 min read
dosfsck command in Linux with Examples dosfsck stands for DOS File System Consistency Check and is part of the dosfstools package in Unix-like operating systems. This command diagnoses and repairs MS-DOS filesystems (FAT12, FAT16, FAT32). Before running dosfsck, it is crucial to unmount the filesystem to avoid data corruption.Syntax:dosf
4 min read
dstat Command in Linux With Examples dstat is a tool that is used to retrieve information or statistics form components of the system such as network connections, IO devices, or CPU, etc. It is generally used by system administrators to retrieve a handful of information about the above-mentioned components of the system. It itself perf
3 min read
du Command in LINUX The du command in Linux is a powerful utility that allows users to analyze and report on disk usage within directories and files. Whether youâre trying to identify space-hogging directories, manage disk space efficiently, or simply gain insights into storage consumption, the du command provides valu
4 min read
dump command in Linux with examples The dump command in Linux is used for backing up the filesystem to a storage device. Unlike commands that back up individual files, dump operates on entire filesystems, transferring data to tape, disk, or other storage media for safekeeping. One of its key advantages is the ability to perform increm
5 min read
dumpe2fs command in Linux with examples dumpe2fs command is used to print the super block and blocks group information for the filesystem present on device. Can be used with ext2/ext3/ext4 filesystem for information. The printed information may be old or inconsistent when it is used with a mounted filesystem. Don't forget to unmount your
2 min read
dumpkeys command in Linux with examples 'dumpkeys' command in Linux is used for the dump keyboard translation tables. It writes to the standard output and the current contents of the keyboard driver's translation tables in the format specified by Keymaps. Part of the kbd package, it displays the key bindings, function key strings, and com
4 min read
echo command in Linux with Examples The echo command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments. It is commonly used in shell scripts and batch files to output status text to the screen or a file. Syntax of `echo` command in Linuxecho [option] [string]Here, [option
3 min read
ed command in Linux with examples Linux 'ed' command is used to start the ed text editor, a very minimal fronted line-based text editor. The lines are very simple in relation to other text files, hence easy to work on, for creating as well as editing, display, and manipulation of files. Since it was the first editor that was include
4 min read
egrep command in Linux with examples The 'egrep' command in Linux is a powerful pattern-searching utility that belongs to the family of grep functions. It functions similarly to 'grep -E' by treating patterns as extended regular expressions, allowing more complex pattern matching without the need to escape special characters. This comm
3 min read