How to run a program in a clean environment in bash?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
89
down vote

favorite
20












I want to run a program in an empty environment (i.e. with no envariables set). How to do this in bash?










share|improve this question

















  • 1




    Possibly related: How to get a clean environment in a ksh shell
    – rahmu
    Sep 25 '12 at 9:14














up vote
89
down vote

favorite
20












I want to run a program in an empty environment (i.e. with no envariables set). How to do this in bash?










share|improve this question

















  • 1




    Possibly related: How to get a clean environment in a ksh shell
    – rahmu
    Sep 25 '12 at 9:14












up vote
89
down vote

favorite
20









up vote
89
down vote

favorite
20






20





I want to run a program in an empty environment (i.e. with no envariables set). How to do this in bash?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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










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.






share|improve this answer


















  • 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 like PATH, PWD and SHLVL 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" 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


















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





share|improve this answer


















  • 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, 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











  • 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

















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 stops bash from reading the system-wide or personal shell initialization scripts that would otherwise be read for a login shell.


  • The --norc option stops bash from reading the personal shell initialization scripts that would otherwise be read for an interactive shell.






share|improve this answer




















  • 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










  • 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

















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.






share|improve this answer




















  • 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

















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





share|improve this answer


















  • 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







  • 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

















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."





share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer


















    • 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 like PATH, PWD and SHLVL 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" 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















    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.






    share|improve this answer


















    • 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 like PATH, PWD and SHLVL 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" 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













    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 like PATH, PWD and SHLVL 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" 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













    • 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 like PATH, PWD and SHLVL 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" 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








    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













    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





    share|improve this answer


















    • 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, 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











    • 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














    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





    share|improve this answer


















    • 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, 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











    • 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












    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





    share|improve this answer














    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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, 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











    • 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




      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










    • @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










    • @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










    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 stops bash from reading the system-wide or personal shell initialization scripts that would otherwise be read for a login shell.


    • The --norc option stops bash from reading the personal shell initialization scripts that would otherwise be read for an interactive shell.






    share|improve this answer




















    • 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










    • 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














    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 stops bash from reading the system-wide or personal shell initialization scripts that would otherwise be read for a login shell.


    • The --norc option stops bash from reading the personal shell initialization scripts that would otherwise be read for an interactive shell.






    share|improve this answer




















    • 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










    • 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












    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 stops bash from reading the system-wide or personal shell initialization scripts that would otherwise be read for a login shell.


    • The --norc option stops bash from reading the personal shell initialization scripts that would otherwise be read for an interactive shell.






    share|improve this answer












    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 stops bash from reading the system-wide or personal shell initialization scripts that would otherwise be read for a login shell.


    • The --norc option stops bash from reading the personal shell initialization scripts that would otherwise be read for an interactive shell.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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 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











    • @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











    • @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











    • @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










    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.






    share|improve this answer




















    • 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














    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.






    share|improve this answer




















    • 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












    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jun 24 '16 at 17:05









    Marcin Raczkowski

    15116




    15116











    • 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
















    • 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















    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










    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





    share|improve this answer


















    • 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







    • 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














    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





    share|improve this answer


















    • 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







    • 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












    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





    share|improve this answer














    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 22 at 20:45

























    answered Jun 22 at 20:31









    Elliott Slaughter

    1112




    1112







    • 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







    • 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












    • 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







    • 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







    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










    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."





    share|improve this answer
























      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."





      share|improve this answer






















        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."





        share|improve this answer












        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."






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 16 '14 at 19:01









        Coroos

        30125




        30125



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay