Intro To 'env' Command In Linux
2023-05-19 - By Robert Elder
I use the 'env' command to show the current environment variables. When I run the 'env' command without arguments like this:
env
I'll see a list of environment variables like the ones you see below:
SHELL=/bin/bash
QT_ACCESSIBILITY=1
COLORTERM=truecolor
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
GNOME_SHELL_SESSION_MODE=ubuntu
XMODIFIERS=@im=ibus
DESKTOP_SESSION=ubuntu
GTK_MODULES=gail:atk-bridge
PWD=/home/robert
LOGNAME=robert
XDG_SESSION_DESKTOP=ubuntu
XDG_SESSION_TYPE=x11
XAUTHORITY=/run/user/1000/gdm/Xauthority
WINDOWPATH=2
HOME=/home/robert
USERNAME=robert
LANG=en_US.UTF-8
KICAD_PATH=/usr/share/kicad
XDG_CURRENT_DESKTOP=ubuntu:GNOME
VTE_VERSION=6003
MANAGERPID=2510
LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_SESSION_CLASS=user
TERM=xterm-256color
LESSOPEN=| /usr/bin/lesspipe %s
USER=robert
GNOME_TERMINAL_SERVICE=:1.115
DISPLAY=:0
SHLVL=1
QT_IM_MODULE=ibus
XDG_RUNTIME_DIR=/run/user/1000
JOURNAL_STREAM=8:68632
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
PATH=/home/robert/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin
GDMSESSION=ubuntu
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
_=/usr/bin/env
These environment variables have important effects on how certain programs run.
Running Commands With Extra Environment Variables Using 'env'
You can use the 'env' command to run other commands with overridden environment variables:
env date
Tue 16 May 2023 11:44:56 AM EDT
When I run the 'date' command by itself like this:
date
Tue 16 May 2023 11:45:05 AM EDT
it defaults to Eastern Standard Time, however, I can use the 'env' command to invoke the date command with the 'TZ' environment variable set:
env TZ='America/Los_Angeles' date
to achieve a different result:
Tue 16 May 2023 08:45:20 AM PDT
Using 'env' To Run Commands From A Different Directory
I can also use the '-C' flag to invoke the command as if it ran in a different working directory. All of the following commands were run from the same directory:
Here is the output from the regular 'pwd' command:
pwd
/home/robert/important
And here is the result when 'pwd' is run through the 'env' command:
env pwd
/home/robert/important
Running a command as if we were in the '/home' directory:
env -C /home pwd
/home
Running a command as if we were in the '/run' directory:
env -C /run pwd
/run
Reset All Environment Variables With 'env' Command
By default, the 'env' command inherits the existing environment variables, so if you set a new environment variable with 'env' any other environment variable that were previously set will still be present:
env FOO=VAR env
SHELL=/bin/bash
QT_ACCESSIBILITY=1
USERNAME=robert
LANG=en_US.UTF-8
... Many Other Environment Variables
FOO=VAR
You can unset all of these other environment variables with the '-i' flag:
env -i FOO=VAR env
FOO=VAR
And that's why the 'env' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|