clear out all variables without closing terminal












9















I want to know how to clear all variables which I defined in command prompt without closing terminal ?



for example, if I set a variable in command prompt as:



$ a=1


now I want to delete the variable $a (and many other variables defined in similar way) without closing terminal. I could use unset but it will be hectic if there are large no. of variables










share|improve this question




















  • 2





    exec "$0"maybe - but it's hard to tell what you mean by global.

    – mikeserv
    Dec 10 '14 at 23:07






  • 3





    Why do you want to do that?

    – Hauke Laging
    Dec 10 '14 at 23:12






  • 1





    @HaukeLaging I am working with arrays using script. I sometimes add one element to it. because of which I cant run the script with old values(since array is changed). i have to close the terminal and start a new session again. I hope I made clear. if not please let me know :)

    – Edward Torvalds
    Dec 10 '14 at 23:18








  • 1





    @muru - he gets a list saved to a $var with var=$(declare -p +F); then passes that as an argument to a function which does echo "${2%%=*}". That answer has a few problems with sheer size - but it should work if your environment isn't huge.

    – mikeserv
    Dec 10 '14 at 23:34






  • 3





    I have no idea what you're trying to do. Are you talking about an interactive shell or a script? Variables set in a script executed from an interactive shell don't affect the parent shell. Tell us what you want to achieve, not which dead end you're pursuing.

    – Gilles
    Dec 10 '14 at 23:49
















9















I want to know how to clear all variables which I defined in command prompt without closing terminal ?



for example, if I set a variable in command prompt as:



$ a=1


now I want to delete the variable $a (and many other variables defined in similar way) without closing terminal. I could use unset but it will be hectic if there are large no. of variables










share|improve this question




















  • 2





    exec "$0"maybe - but it's hard to tell what you mean by global.

    – mikeserv
    Dec 10 '14 at 23:07






  • 3





    Why do you want to do that?

    – Hauke Laging
    Dec 10 '14 at 23:12






  • 1





    @HaukeLaging I am working with arrays using script. I sometimes add one element to it. because of which I cant run the script with old values(since array is changed). i have to close the terminal and start a new session again. I hope I made clear. if not please let me know :)

    – Edward Torvalds
    Dec 10 '14 at 23:18








  • 1





    @muru - he gets a list saved to a $var with var=$(declare -p +F); then passes that as an argument to a function which does echo "${2%%=*}". That answer has a few problems with sheer size - but it should work if your environment isn't huge.

    – mikeserv
    Dec 10 '14 at 23:34






  • 3





    I have no idea what you're trying to do. Are you talking about an interactive shell or a script? Variables set in a script executed from an interactive shell don't affect the parent shell. Tell us what you want to achieve, not which dead end you're pursuing.

    – Gilles
    Dec 10 '14 at 23:49














9












9








9


1






I want to know how to clear all variables which I defined in command prompt without closing terminal ?



for example, if I set a variable in command prompt as:



$ a=1


now I want to delete the variable $a (and many other variables defined in similar way) without closing terminal. I could use unset but it will be hectic if there are large no. of variables










share|improve this question
















I want to know how to clear all variables which I defined in command prompt without closing terminal ?



for example, if I set a variable in command prompt as:



$ a=1


now I want to delete the variable $a (and many other variables defined in similar way) without closing terminal. I could use unset but it will be hectic if there are large no. of variables







shell environment-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 11 '14 at 0:40









Braiam

23.6k2077140




23.6k2077140










asked Dec 10 '14 at 23:06









Edward TorvaldsEdward Torvalds

2,59373263




2,59373263








  • 2





    exec "$0"maybe - but it's hard to tell what you mean by global.

    – mikeserv
    Dec 10 '14 at 23:07






  • 3





    Why do you want to do that?

    – Hauke Laging
    Dec 10 '14 at 23:12






  • 1





    @HaukeLaging I am working with arrays using script. I sometimes add one element to it. because of which I cant run the script with old values(since array is changed). i have to close the terminal and start a new session again. I hope I made clear. if not please let me know :)

    – Edward Torvalds
    Dec 10 '14 at 23:18








  • 1





    @muru - he gets a list saved to a $var with var=$(declare -p +F); then passes that as an argument to a function which does echo "${2%%=*}". That answer has a few problems with sheer size - but it should work if your environment isn't huge.

    – mikeserv
    Dec 10 '14 at 23:34






  • 3





    I have no idea what you're trying to do. Are you talking about an interactive shell or a script? Variables set in a script executed from an interactive shell don't affect the parent shell. Tell us what you want to achieve, not which dead end you're pursuing.

    – Gilles
    Dec 10 '14 at 23:49














  • 2





    exec "$0"maybe - but it's hard to tell what you mean by global.

    – mikeserv
    Dec 10 '14 at 23:07






  • 3





    Why do you want to do that?

    – Hauke Laging
    Dec 10 '14 at 23:12






  • 1





    @HaukeLaging I am working with arrays using script. I sometimes add one element to it. because of which I cant run the script with old values(since array is changed). i have to close the terminal and start a new session again. I hope I made clear. if not please let me know :)

    – Edward Torvalds
    Dec 10 '14 at 23:18








  • 1





    @muru - he gets a list saved to a $var with var=$(declare -p +F); then passes that as an argument to a function which does echo "${2%%=*}". That answer has a few problems with sheer size - but it should work if your environment isn't huge.

    – mikeserv
    Dec 10 '14 at 23:34






  • 3





    I have no idea what you're trying to do. Are you talking about an interactive shell or a script? Variables set in a script executed from an interactive shell don't affect the parent shell. Tell us what you want to achieve, not which dead end you're pursuing.

    – Gilles
    Dec 10 '14 at 23:49








2




2





exec "$0"maybe - but it's hard to tell what you mean by global.

– mikeserv
Dec 10 '14 at 23:07





exec "$0"maybe - but it's hard to tell what you mean by global.

– mikeserv
Dec 10 '14 at 23:07




3




3





Why do you want to do that?

– Hauke Laging
Dec 10 '14 at 23:12





Why do you want to do that?

– Hauke Laging
Dec 10 '14 at 23:12




1




1





@HaukeLaging I am working with arrays using script. I sometimes add one element to it. because of which I cant run the script with old values(since array is changed). i have to close the terminal and start a new session again. I hope I made clear. if not please let me know :)

– Edward Torvalds
Dec 10 '14 at 23:18







@HaukeLaging I am working with arrays using script. I sometimes add one element to it. because of which I cant run the script with old values(since array is changed). i have to close the terminal and start a new session again. I hope I made clear. if not please let me know :)

– Edward Torvalds
Dec 10 '14 at 23:18






1




1





@muru - he gets a list saved to a $var with var=$(declare -p +F); then passes that as an argument to a function which does echo "${2%%=*}". That answer has a few problems with sheer size - but it should work if your environment isn't huge.

– mikeserv
Dec 10 '14 at 23:34





@muru - he gets a list saved to a $var with var=$(declare -p +F); then passes that as an argument to a function which does echo "${2%%=*}". That answer has a few problems with sheer size - but it should work if your environment isn't huge.

– mikeserv
Dec 10 '14 at 23:34




3




3





I have no idea what you're trying to do. Are you talking about an interactive shell or a script? Variables set in a script executed from an interactive shell don't affect the parent shell. Tell us what you want to achieve, not which dead end you're pursuing.

– Gilles
Dec 10 '14 at 23:49





I have no idea what you're trying to do. Are you talking about an interactive shell or a script? Variables set in a script executed from an interactive shell don't affect the parent shell. Tell us what you want to achieve, not which dead end you're pursuing.

– Gilles
Dec 10 '14 at 23:49










2 Answers
2






active

oldest

votes


















9














If you do



exec bash


you will use a fresh & new environnement






share|improve this answer
























  • what about exec "$0" ?

    – Edward Torvalds
    Dec 11 '14 at 21:04











  • @edwardtorvalds $0 is the zeroth argument to an executed shell. So, if you're running bash $0 is bash (probably - it might be -bash in a -login shell case depending on version). If you're running a script with a #!/bin/bash bangline, $0 is the path to the script. exec "$0" is probably more flexible, but exec bash is more explicit. If you enter that command at a terminal, bash should recognize it's in an interactive environment and do the equivalent of bash -i - which will get your ~/.bashrc file run as well.

    – mikeserv
    Dec 11 '14 at 21:32





















0














You could use env, which is provided by GNU coreutils (typically preinstalled on GNU/Linux systems):



exec env --ignore-environment "$0"



The exec system call is so that your current process is replaced in-place by the new, environment-less version of your shell.



This has the benefit to also clear any exported environment variables.






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',
    autoActivateHeartbeat: false,
    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%2f172655%2fclear-out-all-variables-without-closing-terminal%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9














    If you do



    exec bash


    you will use a fresh & new environnement






    share|improve this answer
























    • what about exec "$0" ?

      – Edward Torvalds
      Dec 11 '14 at 21:04











    • @edwardtorvalds $0 is the zeroth argument to an executed shell. So, if you're running bash $0 is bash (probably - it might be -bash in a -login shell case depending on version). If you're running a script with a #!/bin/bash bangline, $0 is the path to the script. exec "$0" is probably more flexible, but exec bash is more explicit. If you enter that command at a terminal, bash should recognize it's in an interactive environment and do the equivalent of bash -i - which will get your ~/.bashrc file run as well.

      – mikeserv
      Dec 11 '14 at 21:32


















    9














    If you do



    exec bash


    you will use a fresh & new environnement






    share|improve this answer
























    • what about exec "$0" ?

      – Edward Torvalds
      Dec 11 '14 at 21:04











    • @edwardtorvalds $0 is the zeroth argument to an executed shell. So, if you're running bash $0 is bash (probably - it might be -bash in a -login shell case depending on version). If you're running a script with a #!/bin/bash bangline, $0 is the path to the script. exec "$0" is probably more flexible, but exec bash is more explicit. If you enter that command at a terminal, bash should recognize it's in an interactive environment and do the equivalent of bash -i - which will get your ~/.bashrc file run as well.

      – mikeserv
      Dec 11 '14 at 21:32
















    9












    9








    9







    If you do



    exec bash


    you will use a fresh & new environnement






    share|improve this answer













    If you do



    exec bash


    you will use a fresh & new environnement







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 11 '14 at 19:46









    Gilles QuenotGilles Quenot

    16.2k13951




    16.2k13951













    • what about exec "$0" ?

      – Edward Torvalds
      Dec 11 '14 at 21:04











    • @edwardtorvalds $0 is the zeroth argument to an executed shell. So, if you're running bash $0 is bash (probably - it might be -bash in a -login shell case depending on version). If you're running a script with a #!/bin/bash bangline, $0 is the path to the script. exec "$0" is probably more flexible, but exec bash is more explicit. If you enter that command at a terminal, bash should recognize it's in an interactive environment and do the equivalent of bash -i - which will get your ~/.bashrc file run as well.

      – mikeserv
      Dec 11 '14 at 21:32





















    • what about exec "$0" ?

      – Edward Torvalds
      Dec 11 '14 at 21:04











    • @edwardtorvalds $0 is the zeroth argument to an executed shell. So, if you're running bash $0 is bash (probably - it might be -bash in a -login shell case depending on version). If you're running a script with a #!/bin/bash bangline, $0 is the path to the script. exec "$0" is probably more flexible, but exec bash is more explicit. If you enter that command at a terminal, bash should recognize it's in an interactive environment and do the equivalent of bash -i - which will get your ~/.bashrc file run as well.

      – mikeserv
      Dec 11 '14 at 21:32



















    what about exec "$0" ?

    – Edward Torvalds
    Dec 11 '14 at 21:04





    what about exec "$0" ?

    – Edward Torvalds
    Dec 11 '14 at 21:04













    @edwardtorvalds $0 is the zeroth argument to an executed shell. So, if you're running bash $0 is bash (probably - it might be -bash in a -login shell case depending on version). If you're running a script with a #!/bin/bash bangline, $0 is the path to the script. exec "$0" is probably more flexible, but exec bash is more explicit. If you enter that command at a terminal, bash should recognize it's in an interactive environment and do the equivalent of bash -i - which will get your ~/.bashrc file run as well.

    – mikeserv
    Dec 11 '14 at 21:32







    @edwardtorvalds $0 is the zeroth argument to an executed shell. So, if you're running bash $0 is bash (probably - it might be -bash in a -login shell case depending on version). If you're running a script with a #!/bin/bash bangline, $0 is the path to the script. exec "$0" is probably more flexible, but exec bash is more explicit. If you enter that command at a terminal, bash should recognize it's in an interactive environment and do the equivalent of bash -i - which will get your ~/.bashrc file run as well.

    – mikeserv
    Dec 11 '14 at 21:32















    0














    You could use env, which is provided by GNU coreutils (typically preinstalled on GNU/Linux systems):



    exec env --ignore-environment "$0"



    The exec system call is so that your current process is replaced in-place by the new, environment-less version of your shell.



    This has the benefit to also clear any exported environment variables.






    share|improve this answer




























      0














      You could use env, which is provided by GNU coreutils (typically preinstalled on GNU/Linux systems):



      exec env --ignore-environment "$0"



      The exec system call is so that your current process is replaced in-place by the new, environment-less version of your shell.



      This has the benefit to also clear any exported environment variables.






      share|improve this answer


























        0












        0








        0







        You could use env, which is provided by GNU coreutils (typically preinstalled on GNU/Linux systems):



        exec env --ignore-environment "$0"



        The exec system call is so that your current process is replaced in-place by the new, environment-less version of your shell.



        This has the benefit to also clear any exported environment variables.






        share|improve this answer













        You could use env, which is provided by GNU coreutils (typically preinstalled on GNU/Linux systems):



        exec env --ignore-environment "$0"



        The exec system call is so that your current process is replaced in-place by the new, environment-less version of your shell.



        This has the benefit to also clear any exported environment variables.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 10 mins ago









        user30747user30747

        1212




        1212






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f172655%2fclear-out-all-variables-without-closing-terminal%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

            CARDNET

            Boot-repair Failure: Unable to locate package grub-common:i386

            濃尾地震