How to run a program in a clean environment in bash?
Clash Royale CLAN TAG#URR8PPP
up vote
89
down vote
favorite
I want to run a program in an empty environment (i.e. with no envariables set). How to do this in bash?
bash environment-variables
add a comment |
up vote
89
down vote
favorite
I want to run a program in an empty environment (i.e. with no envariables set). How to do this in bash?
bash environment-variables
1
Possibly related: How to get a clean environment in a ksh shell
– rahmu
Sep 25 '12 at 9:14
add a comment |
up vote
89
down vote
favorite
up vote
89
down vote
favorite
I want to run a program in an empty environment (i.e. with no envariables set). How to do this in bash?
bash environment-variables
I want to run a program in an empty environment (i.e. with no envariables set). How to do this in bash?
bash environment-variables
bash environment-variables
asked Sep 24 '12 at 13:26
Eugene Yarmash
4,64382945
4,64382945
1
Possibly related: How to get a clean environment in a ksh shell
– rahmu
Sep 25 '12 at 9:14
add a comment |
1
Possibly related: How to get a clean environment in a ksh shell
– rahmu
Sep 25 '12 at 9:14
1
1
Possibly related: How to get a clean environment in a ksh shell
– rahmu
Sep 25 '12 at 9:14
Possibly related: How to get a clean environment in a ksh shell
– rahmu
Sep 25 '12 at 9:14
add a comment |
6 Answers
6
active
oldest
votes
up vote
102
down vote
accepted
You can do this with env
:
env -i your_command
Contrary to comments below, this does completely clear out the environment, but it does not prevent your_command
setting new variables. In particular, running a shell will cause the /etc/profile
to run, and the shell may have some built in settings also.
You can check this with:
env -i env
i.e. wipe the environment and then print it. The output will be blank.
4
It doesn't completely clear out the environment:echo 'echo $PATH' > test.sh && chmod u+x test.sh && env -i test.sh
prints/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.
– l0b0
Sep 24 '12 at 14:14
2
However, it seems this is the closest you can get - It seems like variables likePATH
,PWD
andSHLVL
are set automatically by Bash. +1.
– l0b0
Sep 24 '12 at 14:31
3
@I0b0: See my edit.
– ams
Sep 24 '12 at 14:37
3
The PATH variable in the first commenter's script is not in the environment and therefore is not an environment variable. Bash apparently sets its own regular shell variable called PATH if there isn't one exported for it:env -i bash --norc -c "declare -p PATH"
givesdeclare -- PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
. Note the "-x" for exported (and therefore part of the environment) if you export it yourself:env -i bash --norc -c "export PATH; declare -p PATH"
givesdeclare -x PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
– Binary Phile
Jun 22 at 18:42
add a comment |
up vote
24
down vote
env -i somecommand
runs a command in an empty environment, as ams has already mentioned.
A lot of programs rely on some important environment variables, so you may want to retain them:
env -i HOME="$HOME" LC_CTYPE="$LC_ALL:-$LC_CTYPE:-$LANG" PATH="$PATH" USER="$USER" somecommand
Alternatively, you could log in into a small login-time environment.
ssh localhost somecommand
1
Works when running the command on cmdline. How do I put this in shebang?, doesn't seem to work!
– balki
Nov 4 '13 at 17:16
Strange, myenv
doesn't support--
delimitation and doingenv -i FOO=bar -- env
attempts to run a command named--
.
– antak
Jul 27 '16 at 2:16
@antak That should beenv -i -- FOO=bar env
, actually. My bad. Not that the--
is useful since what follows it does not start with-
.
– Gilles
Jul 27 '16 at 8:10
Didn't realize you could always ssh into localhost, that's kinda weird. What's the use case there.
– Edgar Aroutiounian
Mar 12 '17 at 16:04
@EdgarAroutiounian You can SSH to localhost if it's running an SSH server. Why would programmers go through the effort of forbidding it?
– Gilles
Mar 12 '17 at 20:26
|
show 1 more comment
up vote
22
down vote
A "clean" bash
environment may be had with
$ env -i bash --noprofile --norc
The
env -i
command executes the command given to it on the command line without transferring any of the exported environment variables of the old shell environment to the environment of the executed program.The
--noprofile
option stopsbash
from reading the system-wide or personal shell initialization scripts that would otherwise be read for a login shell.The
--norc
option stopsbash
from reading the personal shell initialization scripts that would otherwise be read for an interactive shell.
My env doesn't recognize those options.
– Paulo Carvalho
Jan 23 at 10:12
@PauloCarvalho Your system does not supportenv -i
? What system are you on?
– Kusalananda
Jan 23 at 10:13
Sorry, it works in the shell. I tried to put it in a script and somehow it threw an error.
– Paulo Carvalho
Feb 15 at 9:56
@PauloCarvalho Sorry, I can't see what command you typed in or what the error you got was.
– Kusalananda
Feb 15 at 10:05
Great - just what I was looking for.env
to clean my environment, and--noprofile
to avoid sourcing /etc/profile and friends, and--norc
to avoid sourcing ~/.bashrc and friends.
– Felipe Alvarez
Mar 1 at 0:01
add a comment |
up vote
4
down vote
While the accepted answer is correct, what you usually want to do is to:
env -i bash -l -c "printenv; and any other commands"
This gives you bare but functional bash (same as you'd get when login in non-interactive mode). This for example sets the language, timezone, HOME, etc.
That doesn't quite work becauseenv -i
clearsHOME
, which meansbash -l
can't find your.bash_profile
etc. If you want a shell that's like what you'd get on a fresh login, you need an extra indirection to setHOME
first.
– Elliott Slaughter
Jun 22 at 20:29
See this answer: unix.stackexchange.com/a/451389/157340
– Elliott Slaughter
Jun 22 at 20:46
add a comment |
up vote
1
down vote
The problem with most answers here is that env -i
clears HOME
, so even if you run bash -l
on the inside, it won't read your .bash_profile
etc. If what you're looking for is a shell that acts as if you had just done a fresh login, you'd want this instead:
env -i HOME="$HOME" bash -l -c 'your_command'
Example:
$ export ABC=123
$ env -i HOME="$HOME" bash -l -c 'env' | grep ABC
$ env HOME="$HOME" bash -l -c 'env' | grep ABC
ABC=123
1
Note that a loginbash
shell will run.bash_login
or.bash_profile
. To get a clean environment, use--noprofile
, or setHOME
to a directory that does not have those files. I suppose it depends on what you mean by "clean".
– Kusalananda
Jun 22 at 20:43
1
Yes, if you want a shell with literally nothing in it, just follow the original answer and doenv -i bash -c ...
. This answer is specifically when you want a shell that looks like you just did a fresh login on the machine.
– Elliott Slaughter
Jun 22 at 20:46
add a comment |
up vote
0
down vote
To answer balki's comment (and answering my own question in the process :-):
% echo Environment in calling shell: vars: $(env |wc -l); echo; ./du; echo; cat du
Environment in calling shell: vars: 43
==> This is the environment: vars: 5
PATH="$PATH"
PWD=/Users/nick
SHLVL=1
SOMETHING_TO_KEEP="$USER"
_=/usr/bin/env
==> The end.
#!/usr/bin/env -i SOMETHING_TO_KEEP="$USER" PATH="$PATH" /bin/sh
echo "==> This is the environment: vars:" $(/usr/bin/env | /usr/bin/wc -l)
/usr/bin/env
echo "==> The end."
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
102
down vote
accepted
You can do this with env
:
env -i your_command
Contrary to comments below, this does completely clear out the environment, but it does not prevent your_command
setting new variables. In particular, running a shell will cause the /etc/profile
to run, and the shell may have some built in settings also.
You can check this with:
env -i env
i.e. wipe the environment and then print it. The output will be blank.
4
It doesn't completely clear out the environment:echo 'echo $PATH' > test.sh && chmod u+x test.sh && env -i test.sh
prints/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.
– l0b0
Sep 24 '12 at 14:14
2
However, it seems this is the closest you can get - It seems like variables likePATH
,PWD
andSHLVL
are set automatically by Bash. +1.
– l0b0
Sep 24 '12 at 14:31
3
@I0b0: See my edit.
– ams
Sep 24 '12 at 14:37
3
The PATH variable in the first commenter's script is not in the environment and therefore is not an environment variable. Bash apparently sets its own regular shell variable called PATH if there isn't one exported for it:env -i bash --norc -c "declare -p PATH"
givesdeclare -- PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
. Note the "-x" for exported (and therefore part of the environment) if you export it yourself:env -i bash --norc -c "export PATH; declare -p PATH"
givesdeclare -x PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
– Binary Phile
Jun 22 at 18:42
add a comment |
up vote
102
down vote
accepted
You can do this with env
:
env -i your_command
Contrary to comments below, this does completely clear out the environment, but it does not prevent your_command
setting new variables. In particular, running a shell will cause the /etc/profile
to run, and the shell may have some built in settings also.
You can check this with:
env -i env
i.e. wipe the environment and then print it. The output will be blank.
4
It doesn't completely clear out the environment:echo 'echo $PATH' > test.sh && chmod u+x test.sh && env -i test.sh
prints/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.
– l0b0
Sep 24 '12 at 14:14
2
However, it seems this is the closest you can get - It seems like variables likePATH
,PWD
andSHLVL
are set automatically by Bash. +1.
– l0b0
Sep 24 '12 at 14:31
3
@I0b0: See my edit.
– ams
Sep 24 '12 at 14:37
3
The PATH variable in the first commenter's script is not in the environment and therefore is not an environment variable. Bash apparently sets its own regular shell variable called PATH if there isn't one exported for it:env -i bash --norc -c "declare -p PATH"
givesdeclare -- PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
. Note the "-x" for exported (and therefore part of the environment) if you export it yourself:env -i bash --norc -c "export PATH; declare -p PATH"
givesdeclare -x PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
– Binary Phile
Jun 22 at 18:42
add a comment |
up vote
102
down vote
accepted
up vote
102
down vote
accepted
You can do this with env
:
env -i your_command
Contrary to comments below, this does completely clear out the environment, but it does not prevent your_command
setting new variables. In particular, running a shell will cause the /etc/profile
to run, and the shell may have some built in settings also.
You can check this with:
env -i env
i.e. wipe the environment and then print it. The output will be blank.
You can do this with env
:
env -i your_command
Contrary to comments below, this does completely clear out the environment, but it does not prevent your_command
setting new variables. In particular, running a shell will cause the /etc/profile
to run, and the shell may have some built in settings also.
You can check this with:
env -i env
i.e. wipe the environment and then print it. The output will be blank.
edited Jun 24 '16 at 17:29
heemayl
34.2k371101
34.2k371101
answered Sep 24 '12 at 13:35
ams
4,29211123
4,29211123
4
It doesn't completely clear out the environment:echo 'echo $PATH' > test.sh && chmod u+x test.sh && env -i test.sh
prints/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.
– l0b0
Sep 24 '12 at 14:14
2
However, it seems this is the closest you can get - It seems like variables likePATH
,PWD
andSHLVL
are set automatically by Bash. +1.
– l0b0
Sep 24 '12 at 14:31
3
@I0b0: See my edit.
– ams
Sep 24 '12 at 14:37
3
The PATH variable in the first commenter's script is not in the environment and therefore is not an environment variable. Bash apparently sets its own regular shell variable called PATH if there isn't one exported for it:env -i bash --norc -c "declare -p PATH"
givesdeclare -- PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
. Note the "-x" for exported (and therefore part of the environment) if you export it yourself:env -i bash --norc -c "export PATH; declare -p PATH"
givesdeclare -x PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
– Binary Phile
Jun 22 at 18:42
add a comment |
4
It doesn't completely clear out the environment:echo 'echo $PATH' > test.sh && chmod u+x test.sh && env -i test.sh
prints/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.
– l0b0
Sep 24 '12 at 14:14
2
However, it seems this is the closest you can get - It seems like variables likePATH
,PWD
andSHLVL
are set automatically by Bash. +1.
– l0b0
Sep 24 '12 at 14:31
3
@I0b0: See my edit.
– ams
Sep 24 '12 at 14:37
3
The PATH variable in the first commenter's script is not in the environment and therefore is not an environment variable. Bash apparently sets its own regular shell variable called PATH if there isn't one exported for it:env -i bash --norc -c "declare -p PATH"
givesdeclare -- PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
. Note the "-x" for exported (and therefore part of the environment) if you export it yourself:env -i bash --norc -c "export PATH; declare -p PATH"
givesdeclare -x PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
– Binary Phile
Jun 22 at 18:42
4
4
It doesn't completely clear out the environment:
echo 'echo $PATH' > test.sh && chmod u+x test.sh && env -i test.sh
prints /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.– l0b0
Sep 24 '12 at 14:14
It doesn't completely clear out the environment:
echo 'echo $PATH' > test.sh && chmod u+x test.sh && env -i test.sh
prints /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.– l0b0
Sep 24 '12 at 14:14
2
2
However, it seems this is the closest you can get - It seems like variables like
PATH
, PWD
and SHLVL
are set automatically by Bash. +1.– l0b0
Sep 24 '12 at 14:31
However, it seems this is the closest you can get - It seems like variables like
PATH
, PWD
and SHLVL
are set automatically by Bash. +1.– l0b0
Sep 24 '12 at 14:31
3
3
@I0b0: See my edit.
– ams
Sep 24 '12 at 14:37
@I0b0: See my edit.
– ams
Sep 24 '12 at 14:37
3
3
The PATH variable in the first commenter's script is not in the environment and therefore is not an environment variable. Bash apparently sets its own regular shell variable called PATH if there isn't one exported for it:
env -i bash --norc -c "declare -p PATH"
gives declare -- PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
. Note the "-x" for exported (and therefore part of the environment) if you export it yourself: env -i bash --norc -c "export PATH; declare -p PATH"
gives declare -x PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
– Binary Phile
Jun 22 at 18:42
The PATH variable in the first commenter's script is not in the environment and therefore is not an environment variable. Bash apparently sets its own regular shell variable called PATH if there isn't one exported for it:
env -i bash --norc -c "declare -p PATH"
gives declare -- PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
. Note the "-x" for exported (and therefore part of the environment) if you export it yourself: env -i bash --norc -c "export PATH; declare -p PATH"
gives declare -x PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:."
– Binary Phile
Jun 22 at 18:42
add a comment |
up vote
24
down vote
env -i somecommand
runs a command in an empty environment, as ams has already mentioned.
A lot of programs rely on some important environment variables, so you may want to retain them:
env -i HOME="$HOME" LC_CTYPE="$LC_ALL:-$LC_CTYPE:-$LANG" PATH="$PATH" USER="$USER" somecommand
Alternatively, you could log in into a small login-time environment.
ssh localhost somecommand
1
Works when running the command on cmdline. How do I put this in shebang?, doesn't seem to work!
– balki
Nov 4 '13 at 17:16
Strange, myenv
doesn't support--
delimitation and doingenv -i FOO=bar -- env
attempts to run a command named--
.
– antak
Jul 27 '16 at 2:16
@antak That should beenv -i -- FOO=bar env
, actually. My bad. Not that the--
is useful since what follows it does not start with-
.
– Gilles
Jul 27 '16 at 8:10
Didn't realize you could always ssh into localhost, that's kinda weird. What's the use case there.
– Edgar Aroutiounian
Mar 12 '17 at 16:04
@EdgarAroutiounian You can SSH to localhost if it's running an SSH server. Why would programmers go through the effort of forbidding it?
– Gilles
Mar 12 '17 at 20:26
|
show 1 more comment
up vote
24
down vote
env -i somecommand
runs a command in an empty environment, as ams has already mentioned.
A lot of programs rely on some important environment variables, so you may want to retain them:
env -i HOME="$HOME" LC_CTYPE="$LC_ALL:-$LC_CTYPE:-$LANG" PATH="$PATH" USER="$USER" somecommand
Alternatively, you could log in into a small login-time environment.
ssh localhost somecommand
1
Works when running the command on cmdline. How do I put this in shebang?, doesn't seem to work!
– balki
Nov 4 '13 at 17:16
Strange, myenv
doesn't support--
delimitation and doingenv -i FOO=bar -- env
attempts to run a command named--
.
– antak
Jul 27 '16 at 2:16
@antak That should beenv -i -- FOO=bar env
, actually. My bad. Not that the--
is useful since what follows it does not start with-
.
– Gilles
Jul 27 '16 at 8:10
Didn't realize you could always ssh into localhost, that's kinda weird. What's the use case there.
– Edgar Aroutiounian
Mar 12 '17 at 16:04
@EdgarAroutiounian You can SSH to localhost if it's running an SSH server. Why would programmers go through the effort of forbidding it?
– Gilles
Mar 12 '17 at 20:26
|
show 1 more comment
up vote
24
down vote
up vote
24
down vote
env -i somecommand
runs a command in an empty environment, as ams has already mentioned.
A lot of programs rely on some important environment variables, so you may want to retain them:
env -i HOME="$HOME" LC_CTYPE="$LC_ALL:-$LC_CTYPE:-$LANG" PATH="$PATH" USER="$USER" somecommand
Alternatively, you could log in into a small login-time environment.
ssh localhost somecommand
env -i somecommand
runs a command in an empty environment, as ams has already mentioned.
A lot of programs rely on some important environment variables, so you may want to retain them:
env -i HOME="$HOME" LC_CTYPE="$LC_ALL:-$LC_CTYPE:-$LANG" PATH="$PATH" USER="$USER" somecommand
Alternatively, you could log in into a small login-time environment.
ssh localhost somecommand
edited Apr 13 '17 at 12:37
Community♦
1
1
answered Sep 25 '12 at 0:31
Gilles
523k12610461576
523k12610461576
1
Works when running the command on cmdline. How do I put this in shebang?, doesn't seem to work!
– balki
Nov 4 '13 at 17:16
Strange, myenv
doesn't support--
delimitation and doingenv -i FOO=bar -- env
attempts to run a command named--
.
– antak
Jul 27 '16 at 2:16
@antak That should beenv -i -- FOO=bar env
, actually. My bad. Not that the--
is useful since what follows it does not start with-
.
– Gilles
Jul 27 '16 at 8:10
Didn't realize you could always ssh into localhost, that's kinda weird. What's the use case there.
– Edgar Aroutiounian
Mar 12 '17 at 16:04
@EdgarAroutiounian You can SSH to localhost if it's running an SSH server. Why would programmers go through the effort of forbidding it?
– Gilles
Mar 12 '17 at 20:26
|
show 1 more comment
1
Works when running the command on cmdline. How do I put this in shebang?, doesn't seem to work!
– balki
Nov 4 '13 at 17:16
Strange, myenv
doesn't support--
delimitation and doingenv -i FOO=bar -- env
attempts to run a command named--
.
– antak
Jul 27 '16 at 2:16
@antak That should beenv -i -- FOO=bar env
, actually. My bad. Not that the--
is useful since what follows it does not start with-
.
– Gilles
Jul 27 '16 at 8:10
Didn't realize you could always ssh into localhost, that's kinda weird. What's the use case there.
– Edgar Aroutiounian
Mar 12 '17 at 16:04
@EdgarAroutiounian You can SSH to localhost if it's running an SSH server. Why would programmers go through the effort of forbidding it?
– Gilles
Mar 12 '17 at 20:26
1
1
Works when running the command on cmdline. How do I put this in shebang?, doesn't seem to work!
– balki
Nov 4 '13 at 17:16
Works when running the command on cmdline. How do I put this in shebang?, doesn't seem to work!
– balki
Nov 4 '13 at 17:16
Strange, my
env
doesn't support --
delimitation and doing env -i FOO=bar -- env
attempts to run a command named --
.– antak
Jul 27 '16 at 2:16
Strange, my
env
doesn't support --
delimitation and doing env -i FOO=bar -- env
attempts to run a command named --
.– antak
Jul 27 '16 at 2:16
@antak That should be
env -i -- FOO=bar env
, actually. My bad. Not that the --
is useful since what follows it does not start with -
.– Gilles
Jul 27 '16 at 8:10
@antak That should be
env -i -- FOO=bar env
, actually. My bad. Not that the --
is useful since what follows it does not start with -
.– Gilles
Jul 27 '16 at 8:10
Didn't realize you could always ssh into localhost, that's kinda weird. What's the use case there.
– Edgar Aroutiounian
Mar 12 '17 at 16:04
Didn't realize you could always ssh into localhost, that's kinda weird. What's the use case there.
– Edgar Aroutiounian
Mar 12 '17 at 16:04
@EdgarAroutiounian You can SSH to localhost if it's running an SSH server. Why would programmers go through the effort of forbidding it?
– Gilles
Mar 12 '17 at 20:26
@EdgarAroutiounian You can SSH to localhost if it's running an SSH server. Why would programmers go through the effort of forbidding it?
– Gilles
Mar 12 '17 at 20:26
|
show 1 more comment
up vote
22
down vote
A "clean" bash
environment may be had with
$ env -i bash --noprofile --norc
The
env -i
command executes the command given to it on the command line without transferring any of the exported environment variables of the old shell environment to the environment of the executed program.The
--noprofile
option stopsbash
from reading the system-wide or personal shell initialization scripts that would otherwise be read for a login shell.The
--norc
option stopsbash
from reading the personal shell initialization scripts that would otherwise be read for an interactive shell.
My env doesn't recognize those options.
– Paulo Carvalho
Jan 23 at 10:12
@PauloCarvalho Your system does not supportenv -i
? What system are you on?
– Kusalananda
Jan 23 at 10:13
Sorry, it works in the shell. I tried to put it in a script and somehow it threw an error.
– Paulo Carvalho
Feb 15 at 9:56
@PauloCarvalho Sorry, I can't see what command you typed in or what the error you got was.
– Kusalananda
Feb 15 at 10:05
Great - just what I was looking for.env
to clean my environment, and--noprofile
to avoid sourcing /etc/profile and friends, and--norc
to avoid sourcing ~/.bashrc and friends.
– Felipe Alvarez
Mar 1 at 0:01
add a comment |
up vote
22
down vote
A "clean" bash
environment may be had with
$ env -i bash --noprofile --norc
The
env -i
command executes the command given to it on the command line without transferring any of the exported environment variables of the old shell environment to the environment of the executed program.The
--noprofile
option stopsbash
from reading the system-wide or personal shell initialization scripts that would otherwise be read for a login shell.The
--norc
option stopsbash
from reading the personal shell initialization scripts that would otherwise be read for an interactive shell.
My env doesn't recognize those options.
– Paulo Carvalho
Jan 23 at 10:12
@PauloCarvalho Your system does not supportenv -i
? What system are you on?
– Kusalananda
Jan 23 at 10:13
Sorry, it works in the shell. I tried to put it in a script and somehow it threw an error.
– Paulo Carvalho
Feb 15 at 9:56
@PauloCarvalho Sorry, I can't see what command you typed in or what the error you got was.
– Kusalananda
Feb 15 at 10:05
Great - just what I was looking for.env
to clean my environment, and--noprofile
to avoid sourcing /etc/profile and friends, and--norc
to avoid sourcing ~/.bashrc and friends.
– Felipe Alvarez
Mar 1 at 0:01
add a comment |
up vote
22
down vote
up vote
22
down vote
A "clean" bash
environment may be had with
$ env -i bash --noprofile --norc
The
env -i
command executes the command given to it on the command line without transferring any of the exported environment variables of the old shell environment to the environment of the executed program.The
--noprofile
option stopsbash
from reading the system-wide or personal shell initialization scripts that would otherwise be read for a login shell.The
--norc
option stopsbash
from reading the personal shell initialization scripts that would otherwise be read for an interactive shell.
A "clean" bash
environment may be had with
$ env -i bash --noprofile --norc
The
env -i
command executes the command given to it on the command line without transferring any of the exported environment variables of the old shell environment to the environment of the executed program.The
--noprofile
option stopsbash
from reading the system-wide or personal shell initialization scripts that would otherwise be read for a login shell.The
--norc
option stopsbash
from reading the personal shell initialization scripts that would otherwise be read for an interactive shell.
answered Jun 24 '16 at 17:28
Kusalananda
118k16223364
118k16223364
My env doesn't recognize those options.
– Paulo Carvalho
Jan 23 at 10:12
@PauloCarvalho Your system does not supportenv -i
? What system are you on?
– Kusalananda
Jan 23 at 10:13
Sorry, it works in the shell. I tried to put it in a script and somehow it threw an error.
– Paulo Carvalho
Feb 15 at 9:56
@PauloCarvalho Sorry, I can't see what command you typed in or what the error you got was.
– Kusalananda
Feb 15 at 10:05
Great - just what I was looking for.env
to clean my environment, and--noprofile
to avoid sourcing /etc/profile and friends, and--norc
to avoid sourcing ~/.bashrc and friends.
– Felipe Alvarez
Mar 1 at 0:01
add a comment |
My env doesn't recognize those options.
– Paulo Carvalho
Jan 23 at 10:12
@PauloCarvalho Your system does not supportenv -i
? What system are you on?
– Kusalananda
Jan 23 at 10:13
Sorry, it works in the shell. I tried to put it in a script and somehow it threw an error.
– Paulo Carvalho
Feb 15 at 9:56
@PauloCarvalho Sorry, I can't see what command you typed in or what the error you got was.
– Kusalananda
Feb 15 at 10:05
Great - just what I was looking for.env
to clean my environment, and--noprofile
to avoid sourcing /etc/profile and friends, and--norc
to avoid sourcing ~/.bashrc and friends.
– Felipe Alvarez
Mar 1 at 0:01
My env doesn't recognize those options.
– Paulo Carvalho
Jan 23 at 10:12
My env doesn't recognize those options.
– Paulo Carvalho
Jan 23 at 10:12
@PauloCarvalho Your system does not support
env -i
? What system are you on?– Kusalananda
Jan 23 at 10:13
@PauloCarvalho Your system does not support
env -i
? What system are you on?– Kusalananda
Jan 23 at 10:13
Sorry, it works in the shell. I tried to put it in a script and somehow it threw an error.
– Paulo Carvalho
Feb 15 at 9:56
Sorry, it works in the shell. I tried to put it in a script and somehow it threw an error.
– Paulo Carvalho
Feb 15 at 9:56
@PauloCarvalho Sorry, I can't see what command you typed in or what the error you got was.
– Kusalananda
Feb 15 at 10:05
@PauloCarvalho Sorry, I can't see what command you typed in or what the error you got was.
– Kusalananda
Feb 15 at 10:05
Great - just what I was looking for.
env
to clean my environment, and --noprofile
to avoid sourcing /etc/profile and friends, and --norc
to avoid sourcing ~/.bashrc and friends.– Felipe Alvarez
Mar 1 at 0:01
Great - just what I was looking for.
env
to clean my environment, and --noprofile
to avoid sourcing /etc/profile and friends, and --norc
to avoid sourcing ~/.bashrc and friends.– Felipe Alvarez
Mar 1 at 0:01
add a comment |
up vote
4
down vote
While the accepted answer is correct, what you usually want to do is to:
env -i bash -l -c "printenv; and any other commands"
This gives you bare but functional bash (same as you'd get when login in non-interactive mode). This for example sets the language, timezone, HOME, etc.
That doesn't quite work becauseenv -i
clearsHOME
, which meansbash -l
can't find your.bash_profile
etc. If you want a shell that's like what you'd get on a fresh login, you need an extra indirection to setHOME
first.
– Elliott Slaughter
Jun 22 at 20:29
See this answer: unix.stackexchange.com/a/451389/157340
– Elliott Slaughter
Jun 22 at 20:46
add a comment |
up vote
4
down vote
While the accepted answer is correct, what you usually want to do is to:
env -i bash -l -c "printenv; and any other commands"
This gives you bare but functional bash (same as you'd get when login in non-interactive mode). This for example sets the language, timezone, HOME, etc.
That doesn't quite work becauseenv -i
clearsHOME
, which meansbash -l
can't find your.bash_profile
etc. If you want a shell that's like what you'd get on a fresh login, you need an extra indirection to setHOME
first.
– Elliott Slaughter
Jun 22 at 20:29
See this answer: unix.stackexchange.com/a/451389/157340
– Elliott Slaughter
Jun 22 at 20:46
add a comment |
up vote
4
down vote
up vote
4
down vote
While the accepted answer is correct, what you usually want to do is to:
env -i bash -l -c "printenv; and any other commands"
This gives you bare but functional bash (same as you'd get when login in non-interactive mode). This for example sets the language, timezone, HOME, etc.
While the accepted answer is correct, what you usually want to do is to:
env -i bash -l -c "printenv; and any other commands"
This gives you bare but functional bash (same as you'd get when login in non-interactive mode). This for example sets the language, timezone, HOME, etc.
answered Jun 24 '16 at 17:05
Marcin Raczkowski
15116
15116
That doesn't quite work becauseenv -i
clearsHOME
, which meansbash -l
can't find your.bash_profile
etc. If you want a shell that's like what you'd get on a fresh login, you need an extra indirection to setHOME
first.
– Elliott Slaughter
Jun 22 at 20:29
See this answer: unix.stackexchange.com/a/451389/157340
– Elliott Slaughter
Jun 22 at 20:46
add a comment |
That doesn't quite work becauseenv -i
clearsHOME
, which meansbash -l
can't find your.bash_profile
etc. If you want a shell that's like what you'd get on a fresh login, you need an extra indirection to setHOME
first.
– Elliott Slaughter
Jun 22 at 20:29
See this answer: unix.stackexchange.com/a/451389/157340
– Elliott Slaughter
Jun 22 at 20:46
That doesn't quite work because
env -i
clears HOME
, which means bash -l
can't find your .bash_profile
etc. If you want a shell that's like what you'd get on a fresh login, you need an extra indirection to set HOME
first.– Elliott Slaughter
Jun 22 at 20:29
That doesn't quite work because
env -i
clears HOME
, which means bash -l
can't find your .bash_profile
etc. If you want a shell that's like what you'd get on a fresh login, you need an extra indirection to set HOME
first.– Elliott Slaughter
Jun 22 at 20:29
See this answer: unix.stackexchange.com/a/451389/157340
– Elliott Slaughter
Jun 22 at 20:46
See this answer: unix.stackexchange.com/a/451389/157340
– Elliott Slaughter
Jun 22 at 20:46
add a comment |
up vote
1
down vote
The problem with most answers here is that env -i
clears HOME
, so even if you run bash -l
on the inside, it won't read your .bash_profile
etc. If what you're looking for is a shell that acts as if you had just done a fresh login, you'd want this instead:
env -i HOME="$HOME" bash -l -c 'your_command'
Example:
$ export ABC=123
$ env -i HOME="$HOME" bash -l -c 'env' | grep ABC
$ env HOME="$HOME" bash -l -c 'env' | grep ABC
ABC=123
1
Note that a loginbash
shell will run.bash_login
or.bash_profile
. To get a clean environment, use--noprofile
, or setHOME
to a directory that does not have those files. I suppose it depends on what you mean by "clean".
– Kusalananda
Jun 22 at 20:43
1
Yes, if you want a shell with literally nothing in it, just follow the original answer and doenv -i bash -c ...
. This answer is specifically when you want a shell that looks like you just did a fresh login on the machine.
– Elliott Slaughter
Jun 22 at 20:46
add a comment |
up vote
1
down vote
The problem with most answers here is that env -i
clears HOME
, so even if you run bash -l
on the inside, it won't read your .bash_profile
etc. If what you're looking for is a shell that acts as if you had just done a fresh login, you'd want this instead:
env -i HOME="$HOME" bash -l -c 'your_command'
Example:
$ export ABC=123
$ env -i HOME="$HOME" bash -l -c 'env' | grep ABC
$ env HOME="$HOME" bash -l -c 'env' | grep ABC
ABC=123
1
Note that a loginbash
shell will run.bash_login
or.bash_profile
. To get a clean environment, use--noprofile
, or setHOME
to a directory that does not have those files. I suppose it depends on what you mean by "clean".
– Kusalananda
Jun 22 at 20:43
1
Yes, if you want a shell with literally nothing in it, just follow the original answer and doenv -i bash -c ...
. This answer is specifically when you want a shell that looks like you just did a fresh login on the machine.
– Elliott Slaughter
Jun 22 at 20:46
add a comment |
up vote
1
down vote
up vote
1
down vote
The problem with most answers here is that env -i
clears HOME
, so even if you run bash -l
on the inside, it won't read your .bash_profile
etc. If what you're looking for is a shell that acts as if you had just done a fresh login, you'd want this instead:
env -i HOME="$HOME" bash -l -c 'your_command'
Example:
$ export ABC=123
$ env -i HOME="$HOME" bash -l -c 'env' | grep ABC
$ env HOME="$HOME" bash -l -c 'env' | grep ABC
ABC=123
The problem with most answers here is that env -i
clears HOME
, so even if you run bash -l
on the inside, it won't read your .bash_profile
etc. If what you're looking for is a shell that acts as if you had just done a fresh login, you'd want this instead:
env -i HOME="$HOME" bash -l -c 'your_command'
Example:
$ export ABC=123
$ env -i HOME="$HOME" bash -l -c 'env' | grep ABC
$ env HOME="$HOME" bash -l -c 'env' | grep ABC
ABC=123
edited Jun 22 at 20:45
answered Jun 22 at 20:31
Elliott Slaughter
1112
1112
1
Note that a loginbash
shell will run.bash_login
or.bash_profile
. To get a clean environment, use--noprofile
, or setHOME
to a directory that does not have those files. I suppose it depends on what you mean by "clean".
– Kusalananda
Jun 22 at 20:43
1
Yes, if you want a shell with literally nothing in it, just follow the original answer and doenv -i bash -c ...
. This answer is specifically when you want a shell that looks like you just did a fresh login on the machine.
– Elliott Slaughter
Jun 22 at 20:46
add a comment |
1
Note that a loginbash
shell will run.bash_login
or.bash_profile
. To get a clean environment, use--noprofile
, or setHOME
to a directory that does not have those files. I suppose it depends on what you mean by "clean".
– Kusalananda
Jun 22 at 20:43
1
Yes, if you want a shell with literally nothing in it, just follow the original answer and doenv -i bash -c ...
. This answer is specifically when you want a shell that looks like you just did a fresh login on the machine.
– Elliott Slaughter
Jun 22 at 20:46
1
1
Note that a login
bash
shell will run .bash_login
or .bash_profile
. To get a clean environment, use --noprofile
, or set HOME
to a directory that does not have those files. I suppose it depends on what you mean by "clean".– Kusalananda
Jun 22 at 20:43
Note that a login
bash
shell will run .bash_login
or .bash_profile
. To get a clean environment, use --noprofile
, or set HOME
to a directory that does not have those files. I suppose it depends on what you mean by "clean".– Kusalananda
Jun 22 at 20:43
1
1
Yes, if you want a shell with literally nothing in it, just follow the original answer and do
env -i bash -c ...
. This answer is specifically when you want a shell that looks like you just did a fresh login on the machine.– Elliott Slaughter
Jun 22 at 20:46
Yes, if you want a shell with literally nothing in it, just follow the original answer and do
env -i bash -c ...
. This answer is specifically when you want a shell that looks like you just did a fresh login on the machine.– Elliott Slaughter
Jun 22 at 20:46
add a comment |
up vote
0
down vote
To answer balki's comment (and answering my own question in the process :-):
% echo Environment in calling shell: vars: $(env |wc -l); echo; ./du; echo; cat du
Environment in calling shell: vars: 43
==> This is the environment: vars: 5
PATH="$PATH"
PWD=/Users/nick
SHLVL=1
SOMETHING_TO_KEEP="$USER"
_=/usr/bin/env
==> The end.
#!/usr/bin/env -i SOMETHING_TO_KEEP="$USER" PATH="$PATH" /bin/sh
echo "==> This is the environment: vars:" $(/usr/bin/env | /usr/bin/wc -l)
/usr/bin/env
echo "==> The end."
add a comment |
up vote
0
down vote
To answer balki's comment (and answering my own question in the process :-):
% echo Environment in calling shell: vars: $(env |wc -l); echo; ./du; echo; cat du
Environment in calling shell: vars: 43
==> This is the environment: vars: 5
PATH="$PATH"
PWD=/Users/nick
SHLVL=1
SOMETHING_TO_KEEP="$USER"
_=/usr/bin/env
==> The end.
#!/usr/bin/env -i SOMETHING_TO_KEEP="$USER" PATH="$PATH" /bin/sh
echo "==> This is the environment: vars:" $(/usr/bin/env | /usr/bin/wc -l)
/usr/bin/env
echo "==> The end."
add a comment |
up vote
0
down vote
up vote
0
down vote
To answer balki's comment (and answering my own question in the process :-):
% echo Environment in calling shell: vars: $(env |wc -l); echo; ./du; echo; cat du
Environment in calling shell: vars: 43
==> This is the environment: vars: 5
PATH="$PATH"
PWD=/Users/nick
SHLVL=1
SOMETHING_TO_KEEP="$USER"
_=/usr/bin/env
==> The end.
#!/usr/bin/env -i SOMETHING_TO_KEEP="$USER" PATH="$PATH" /bin/sh
echo "==> This is the environment: vars:" $(/usr/bin/env | /usr/bin/wc -l)
/usr/bin/env
echo "==> The end."
To answer balki's comment (and answering my own question in the process :-):
% echo Environment in calling shell: vars: $(env |wc -l); echo; ./du; echo; cat du
Environment in calling shell: vars: 43
==> This is the environment: vars: 5
PATH="$PATH"
PWD=/Users/nick
SHLVL=1
SOMETHING_TO_KEEP="$USER"
_=/usr/bin/env
==> The end.
#!/usr/bin/env -i SOMETHING_TO_KEEP="$USER" PATH="$PATH" /bin/sh
echo "==> This is the environment: vars:" $(/usr/bin/env | /usr/bin/wc -l)
/usr/bin/env
echo "==> The end."
answered May 16 '14 at 19:01
Coroos
30125
30125
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f48994%2fhow-to-run-a-program-in-a-clean-environment-in-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Possibly related: How to get a clean environment in a ksh shell
– rahmu
Sep 25 '12 at 9:14