Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unset an environment variable for a single command

In Bash, we can set an environment variable for a single command this way:

FOO=bar somecommand 

What if we want to unset a variable for a single command?

like image 708
skeptical scientist Avatar asked Sep 19 '13 23:09

skeptical scientist


People also ask

How do you unset an environment variable?

To unset an environment variable from Command Prompt, type the command setx variable_name “”. For example, we typed setx TEST “” and this environment variable now had an empty value.

How do I set an environment variable for a single command?

We can set variables for a single command using this syntax: VAR1=VALUE1 VAR2=VALUE2 ... Command ARG1 ARG2...

How do I unset a variable in terminal?

To unset the variable simply set the value of variable to '' . c.) Here, we created a local variable VAR2 and set it to a value. Then in-order to run a command temporarily clearing out all local and other environment variables, we executed 'env –i' command.

Which command can be used to remove an environmental variable?

The dsadmin command can be used for deleting an environment variable in a particular project.


2 Answers

Technically, they're not environment variables until someone exports them. But you can at least set them to empty:

FOO= some command 

If removing them from the environment is enough, you can use env:

env -u FOO somecommand 
like image 50
JB. Avatar answered Sep 28 '22 09:09

JB.


env -u FOO somecommand 

This will remove the environment variable FOO from the somecommand process' environment.

And to unset multiple variables:

env -u FOO -u FOO2 somecommand 
like image 35
qwertzguy Avatar answered Sep 28 '22 08:09

qwertzguy