See all GNU/Linux related FAQ
I am new to using the bash shell and need to temporarily clear environment variables without deleting or unsetting exported variables. How can I run a program in a modified environment in bash or ksh shell? Is it possible to unset a runtime variable for a single command at the CLI?

You can use the env command to set and print the environment on a Linux or Unix-like system. The env command executes utility after modifying the environment as specified on the command line. The same command can be used to temporarily clear Bash environment variables and unset specific variables for a single command or your scripts.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category Linux shell scripting
Prerequisites Bash
OS compatibility BSD Linux macOS Unix
Est. reading time 4 minutes

How do I display my current environment?

Open the terminal application and type any (env command or printenv command) of the following command:
$ printenv
## OR ##
$ env

Here is what I see:

Fig.01: Unix/Linux: List All Environment Variables Command

Fig.01: Unix/Linux: List All Environment Variables Command

Please note that all examples tested on Bash version 5.x.xx running on macOS and Linux desktops.

Counting your environment variables in Linux and Unix

Use the combination of env/printenv and wc command as follows:
$ env | wc -l
$ printenv | wc -l

Sample outputs:


The env command prints the current environment variables to the screen. The wc -l command counts the number of lines on the screen.

Run a program in a clean environment in bash/ksh/zsh

The syntax is as follows to start with an empty environment
$ env -i <your-program-name-here> [arg1] [arg2] ...
For example, run the wget program without using http_proxy and/or all other variables i.e. temporarily clear all bash/ksh/zsh environment variables and run the wget program:
$ env -i /usr/local/bin/wget
$ env -i wget

This is very useful when you want to run a command ignoring any environment variables you have set. I use this command many times everyday to ignore the http_proxy and other environment variable I have set at the CLI session.

Example: Run the wget command with the http_proxy

$ wget

--2015-08-03 23:20:23--
Connecting to connected.
Proxy request sent, awaiting response... 200 OK
Length: unspecified 
Saving to: 'index.html'

index.html                 [  <=>                         ]  36.17K  87.0KB/s   in 0.4s   

2015-08-03 23:20:24 (87.0 KB/s) - 'index.html' saved [37041]

Example: Ignore the http_proxy when using the wget command

Run it as follows:
$ env -i /usr/local/bin/wget

--2015-08-03 23:25:17--
Connecting to||:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified 
Saving to: 'index.html.1'

index.html.1               [  <=>                         ]  36.17K   115KB/s   in 0.3s   

2015-08-03 23:25:18 (115 KB/s) - 'index.html.1' saved [37041]

The option -i causes env command to completely ignore the environment it inherits. However, it does not prevent your command (such as wget or curl) setting new variables. Also, note down the side effect of running bash/ksh shell:
$ env -i env | wc -l ## empty ##
# Now run bash ##
$ env -i bash
## New environment set by bash program ##
$ env | wc -l

Setting an environmental variable

Do you need to set the run time environment variable just for your command and script? It is like a one-time deal (set a single-use variable). The syntax is:
$ env var=value </path/to/command> arg1 arg2 ...
## OR ##
$ var=value </path/to/command> arg1 arg2 ...

For example set http_proxy for the wget command:

env http_proxy="" \


http_proxy="" \

Removing variable from the environment

Pass the -u to the env command. For example:
$ env -u <VARIABLE> command1 arg1
# Remove the http_proxy variable for the wget
$ env -u http_proxy wget url1

Unset an environment variable for a single command and impact of the export command

So how do you remove an environment variable temporarily for a specific command, and what effect does the “export” command have? The answer is simple, your commands or scripts do not use shell variables unless you export them using the export command. Let us create a sample shell script named

echo "Unset an environment variable for a single command and export command effect"
echo "DEST variable is $DEST"
echo "IP_HOST variable is $IP_HOST"
echo "DEST_USER variable is $DEST_USER"

Here is how to run your .sh script without any variables set:
$ chmod +x
$ ./

Now set those three variables at the CLI and run the script:
$ DEST="/backups/"
$ IP_HOST=""
$ DEST_USER="nixcraft"
$ ./

You will get empty outputs as they are not environment variables until you export them. So export it and rerun the script:
$ export DEST
$ export IP_HOST
$ export DEST_USER
$ ./

Here is how to temporarily clear out all bash environment variables

$ env -i ./
You can also unset an environment variable for a single command, say remove the DEST_USER variable:
$ env -u DEST_USER ./
It is possible to override the variable with a new value at the runtime too (change the current value from “nixcraft” to “vivek” for the DEST_USER variable):
$ DEST_USER="vivek" ./

How To Temporarily Clear ALL or Specific Bash Environment Variables on a Linux and Unix-like System

Click to enlarge

Summing up

The env command is powerful bash command that can set the environment and run COMMAND. It allows you to modify the environment too. You can start with an empty environment by passing the -i option:

env -i /path/to/script
env -i /path/to/command -arg1 -arg2

It is possible to unset or remove a variable from the environment for specific session or commands too by using the -u VAR_NAME option:

env -u var_name /path/to/script
env -u var_name1 -u var_name2 /path/to/command1 -opt1

Finally, you can override the run time variable using the following syntax as long as those variables are not marked as read-only constant by the readonly command:

var_name="NEW_VALUE" /path/to/script

The implementation of the env command command may vary between shells. Refer to manual pages using man command or help command as follows:
$ man bash
$ man zsh
$ man ksh
#Bash specific help#
$ env --help
$ help readonly
$ help export

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

2 comments… add one
  • sat Aug 11, 2015 @ 7:52

    Nice one..! It will be very useful.

    Thank you.

    • Anonymous Aug 21, 2023 @ 6:06

      Eight years later, the page is still useful to me. Thank you for publishing it and keeping it real.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.