Temporarily declare a variable in Bash












0















To declare a variable in Bash, say in a Bash script-file (that doesn't include Bash function), I do for example x=y and when I finish using it inside that script-file I do unset x.



Is there a way (without using a function), to unset the variable after say 5 minutes, both in one line? A plausible approach might be x=y && echo "unset x" | at now + 5 minutes.



In this particular case I run the script-file directly in the terminal by copy-pasting its content from GitHub to terminal. This falls under sourcing I assume".





Given I use GitHub, an alternative might be executing a raw version of the script-file directly from GitHub with bash in a separate shell as follows, but I don't like that way because it can't be user/repo/branch/path/file-agnostic:



wget -O - https://raw.githubusercontent.com/<username>/<repo>/<branch>/<path>/<file> | bash









share|improve this question




















  • 1





    You need to do unset x only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like ( for i in {1..20}; do dosomething; done), and after I execute this command I don't have $i in my shell.

    – Weijun Zhou
    Dec 13 '18 at 3:05











  • If you're trying to do that for "security" purposes (ie. that var holds a password), then keep in mind that unsetting a variable does not guarantee in any way that its content will be scrubbed from memory. Simply put, keeping sensitive data in plain text in shell variables is not something that should be done for 5 secs, 5 minutes or 5 hours.

    – pizdelect
    Dec 13 '18 at 4:13








  • 1





    @WeijunZhou I updated the question due to your comment.

    – JohnDoea
    Dec 13 '18 at 15:58
















0















To declare a variable in Bash, say in a Bash script-file (that doesn't include Bash function), I do for example x=y and when I finish using it inside that script-file I do unset x.



Is there a way (without using a function), to unset the variable after say 5 minutes, both in one line? A plausible approach might be x=y && echo "unset x" | at now + 5 minutes.



In this particular case I run the script-file directly in the terminal by copy-pasting its content from GitHub to terminal. This falls under sourcing I assume".





Given I use GitHub, an alternative might be executing a raw version of the script-file directly from GitHub with bash in a separate shell as follows, but I don't like that way because it can't be user/repo/branch/path/file-agnostic:



wget -O - https://raw.githubusercontent.com/<username>/<repo>/<branch>/<path>/<file> | bash









share|improve this question




















  • 1





    You need to do unset x only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like ( for i in {1..20}; do dosomething; done), and after I execute this command I don't have $i in my shell.

    – Weijun Zhou
    Dec 13 '18 at 3:05











  • If you're trying to do that for "security" purposes (ie. that var holds a password), then keep in mind that unsetting a variable does not guarantee in any way that its content will be scrubbed from memory. Simply put, keeping sensitive data in plain text in shell variables is not something that should be done for 5 secs, 5 minutes or 5 hours.

    – pizdelect
    Dec 13 '18 at 4:13








  • 1





    @WeijunZhou I updated the question due to your comment.

    – JohnDoea
    Dec 13 '18 at 15:58














0












0








0


2






To declare a variable in Bash, say in a Bash script-file (that doesn't include Bash function), I do for example x=y and when I finish using it inside that script-file I do unset x.



Is there a way (without using a function), to unset the variable after say 5 minutes, both in one line? A plausible approach might be x=y && echo "unset x" | at now + 5 minutes.



In this particular case I run the script-file directly in the terminal by copy-pasting its content from GitHub to terminal. This falls under sourcing I assume".





Given I use GitHub, an alternative might be executing a raw version of the script-file directly from GitHub with bash in a separate shell as follows, but I don't like that way because it can't be user/repo/branch/path/file-agnostic:



wget -O - https://raw.githubusercontent.com/<username>/<repo>/<branch>/<path>/<file> | bash









share|improve this question
















To declare a variable in Bash, say in a Bash script-file (that doesn't include Bash function), I do for example x=y and when I finish using it inside that script-file I do unset x.



Is there a way (without using a function), to unset the variable after say 5 minutes, both in one line? A plausible approach might be x=y && echo "unset x" | at now + 5 minutes.



In this particular case I run the script-file directly in the terminal by copy-pasting its content from GitHub to terminal. This falls under sourcing I assume".





Given I use GitHub, an alternative might be executing a raw version of the script-file directly from GitHub with bash in a separate shell as follows, but I don't like that way because it can't be user/repo/branch/path/file-agnostic:



wget -O - https://raw.githubusercontent.com/<username>/<repo>/<branch>/<path>/<file> | bash






bash variable at






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 14 '18 at 17:42







JohnDoea

















asked Dec 13 '18 at 1:52









JohnDoeaJohnDoea

601132




601132








  • 1





    You need to do unset x only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like ( for i in {1..20}; do dosomething; done), and after I execute this command I don't have $i in my shell.

    – Weijun Zhou
    Dec 13 '18 at 3:05











  • If you're trying to do that for "security" purposes (ie. that var holds a password), then keep in mind that unsetting a variable does not guarantee in any way that its content will be scrubbed from memory. Simply put, keeping sensitive data in plain text in shell variables is not something that should be done for 5 secs, 5 minutes or 5 hours.

    – pizdelect
    Dec 13 '18 at 4:13








  • 1





    @WeijunZhou I updated the question due to your comment.

    – JohnDoea
    Dec 13 '18 at 15:58














  • 1





    You need to do unset x only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like ( for i in {1..20}; do dosomething; done), and after I execute this command I don't have $i in my shell.

    – Weijun Zhou
    Dec 13 '18 at 3:05











  • If you're trying to do that for "security" purposes (ie. that var holds a password), then keep in mind that unsetting a variable does not guarantee in any way that its content will be scrubbed from memory. Simply put, keeping sensitive data in plain text in shell variables is not something that should be done for 5 secs, 5 minutes or 5 hours.

    – pizdelect
    Dec 13 '18 at 4:13








  • 1





    @WeijunZhou I updated the question due to your comment.

    – JohnDoea
    Dec 13 '18 at 15:58








1




1





You need to do unset x only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like ( for i in {1..20}; do dosomething; done), and after I execute this command I don't have $i in my shell.

– Weijun Zhou
Dec 13 '18 at 3:05





You need to do unset x only if you are sourcing the script file. Otherwise the script runs in a subshell and does not affect your shell environment, so you don't need to unset it. I use parentheses to run one-liners with temporary variables on the command line all the time, like ( for i in {1..20}; do dosomething; done), and after I execute this command I don't have $i in my shell.

– Weijun Zhou
Dec 13 '18 at 3:05













If you're trying to do that for "security" purposes (ie. that var holds a password), then keep in mind that unsetting a variable does not guarantee in any way that its content will be scrubbed from memory. Simply put, keeping sensitive data in plain text in shell variables is not something that should be done for 5 secs, 5 minutes or 5 hours.

– pizdelect
Dec 13 '18 at 4:13







If you're trying to do that for "security" purposes (ie. that var holds a password), then keep in mind that unsetting a variable does not guarantee in any way that its content will be scrubbed from memory. Simply put, keeping sensitive data in plain text in shell variables is not something that should be done for 5 secs, 5 minutes or 5 hours.

– pizdelect
Dec 13 '18 at 4:13






1




1





@WeijunZhou I updated the question due to your comment.

– JohnDoea
Dec 13 '18 at 15:58





@WeijunZhou I updated the question due to your comment.

– JohnDoea
Dec 13 '18 at 15:58










4 Answers
4






active

oldest

votes


















6














You can't use at jobs because they run in a different context, and can't affect the current shell.



But we can do something similar. This code will trigger an alarm signal, which we can catch and perform action on



#!/bin/bash

x=100

trap 'unset x' SIGALRM

mypid=$$

( /bin/sleep 3 ; kill -ALRM $mypid) &

for a in 1 2 3 4 5 6
do
echo Now x=$x
sleep 1
done


This example is only 3 seconds long to demonstrate the solution; you can pick your delay as you need.



In action:



Now x=100
Now x=100
Now x=100
Now x=
Now x=
Now x=


You can easily make it one line with ;...






share|improve this answer


























  • Wow - great minds think alike -- and within 30 seconds of each other! :)

    – Jeff Schaller
    Dec 13 '18 at 2:09











  • @JeffSchaller I noticed that :-)

    – Stephen Harris
    Dec 13 '18 at 2:10








  • 1





    @JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean the for loop won't demonstrate the solution working. I'll edit the answer to make that clear.

    – Stephen Harris
    Dec 13 '18 at 2:14





















6














Sure:



trap 'unset x; trap - USR1' USR1; { sleep 5m; kill -USR1 $$; } &


This sets a trap on the USR1 signal, then (cheating with a semicolon to put it on one line) groups together the sleep and kill commands into a background job. When that job completes, it will send the USR1 signal to the current shell, which will execute the trap. The trap unsets x and then clears the trap.



No functions!






share|improve this answer

































    1














    With your revised question, based around the command line and not a script, your simplest solution is to use a subshell to do your work.



    eg



    $ bash
    $ x=100
    $ echo $x
    100
    $ exit
    $ echo $x

    $


    All the variables you set between running bash and exiting that shell will be forgotten.



    This is very close to how the wget ... | bash part works as well, except you can cut'n'paste/type commands.






    share|improve this answer































      0














      Actually the simplest way might be to just use a compound command with a variable declaration at the start.



      If you declare a variable at the start of a command line, the variable is temporarily defined only for the scope of that command.



      madumlao@lezard:~$ x=foo
      madumlao@lezard:~$ x=y bash -c 'echo $x'
      y
      madumlao@lezard:~$ echo $x
      foo


      As you can see, the line with a variable declaration only sets the variable in the environment for that line only.



      Note that the following will not work:



      madumlao@lezard:~$ x=foo
      madumlao@lezard:~$ x=y echo $x

      madumlao@lezard:~$ echo $x
      foo


      The $x in the second line is parsed during command-line parsing, before the value is actually assigned, so it is unexpectedly blank!






      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%2f487683%2ftemporarily-declare-a-variable-in-bash%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        6














        You can't use at jobs because they run in a different context, and can't affect the current shell.



        But we can do something similar. This code will trigger an alarm signal, which we can catch and perform action on



        #!/bin/bash

        x=100

        trap 'unset x' SIGALRM

        mypid=$$

        ( /bin/sleep 3 ; kill -ALRM $mypid) &

        for a in 1 2 3 4 5 6
        do
        echo Now x=$x
        sleep 1
        done


        This example is only 3 seconds long to demonstrate the solution; you can pick your delay as you need.



        In action:



        Now x=100
        Now x=100
        Now x=100
        Now x=
        Now x=
        Now x=


        You can easily make it one line with ;...






        share|improve this answer


























        • Wow - great minds think alike -- and within 30 seconds of each other! :)

          – Jeff Schaller
          Dec 13 '18 at 2:09











        • @JeffSchaller I noticed that :-)

          – Stephen Harris
          Dec 13 '18 at 2:10








        • 1





          @JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean the for loop won't demonstrate the solution working. I'll edit the answer to make that clear.

          – Stephen Harris
          Dec 13 '18 at 2:14


















        6














        You can't use at jobs because they run in a different context, and can't affect the current shell.



        But we can do something similar. This code will trigger an alarm signal, which we can catch and perform action on



        #!/bin/bash

        x=100

        trap 'unset x' SIGALRM

        mypid=$$

        ( /bin/sleep 3 ; kill -ALRM $mypid) &

        for a in 1 2 3 4 5 6
        do
        echo Now x=$x
        sleep 1
        done


        This example is only 3 seconds long to demonstrate the solution; you can pick your delay as you need.



        In action:



        Now x=100
        Now x=100
        Now x=100
        Now x=
        Now x=
        Now x=


        You can easily make it one line with ;...






        share|improve this answer


























        • Wow - great minds think alike -- and within 30 seconds of each other! :)

          – Jeff Schaller
          Dec 13 '18 at 2:09











        • @JeffSchaller I noticed that :-)

          – Stephen Harris
          Dec 13 '18 at 2:10








        • 1





          @JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean the for loop won't demonstrate the solution working. I'll edit the answer to make that clear.

          – Stephen Harris
          Dec 13 '18 at 2:14
















        6












        6








        6







        You can't use at jobs because they run in a different context, and can't affect the current shell.



        But we can do something similar. This code will trigger an alarm signal, which we can catch and perform action on



        #!/bin/bash

        x=100

        trap 'unset x' SIGALRM

        mypid=$$

        ( /bin/sleep 3 ; kill -ALRM $mypid) &

        for a in 1 2 3 4 5 6
        do
        echo Now x=$x
        sleep 1
        done


        This example is only 3 seconds long to demonstrate the solution; you can pick your delay as you need.



        In action:



        Now x=100
        Now x=100
        Now x=100
        Now x=
        Now x=
        Now x=


        You can easily make it one line with ;...






        share|improve this answer















        You can't use at jobs because they run in a different context, and can't affect the current shell.



        But we can do something similar. This code will trigger an alarm signal, which we can catch and perform action on



        #!/bin/bash

        x=100

        trap 'unset x' SIGALRM

        mypid=$$

        ( /bin/sleep 3 ; kill -ALRM $mypid) &

        for a in 1 2 3 4 5 6
        do
        echo Now x=$x
        sleep 1
        done


        This example is only 3 seconds long to demonstrate the solution; you can pick your delay as you need.



        In action:



        Now x=100
        Now x=100
        Now x=100
        Now x=
        Now x=
        Now x=


        You can easily make it one line with ;...







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 13 '18 at 2:15

























        answered Dec 13 '18 at 2:02









        Stephen HarrisStephen Harris

        25.4k24477




        25.4k24477













        • Wow - great minds think alike -- and within 30 seconds of each other! :)

          – Jeff Schaller
          Dec 13 '18 at 2:09











        • @JeffSchaller I noticed that :-)

          – Stephen Harris
          Dec 13 '18 at 2:10








        • 1





          @JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean the for loop won't demonstrate the solution working. I'll edit the answer to make that clear.

          – Stephen Harris
          Dec 13 '18 at 2:14





















        • Wow - great minds think alike -- and within 30 seconds of each other! :)

          – Jeff Schaller
          Dec 13 '18 at 2:09











        • @JeffSchaller I noticed that :-)

          – Stephen Harris
          Dec 13 '18 at 2:10








        • 1





          @JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean the for loop won't demonstrate the solution working. I'll edit the answer to make that clear.

          – Stephen Harris
          Dec 13 '18 at 2:14



















        Wow - great minds think alike -- and within 30 seconds of each other! :)

        – Jeff Schaller
        Dec 13 '18 at 2:09





        Wow - great minds think alike -- and within 30 seconds of each other! :)

        – Jeff Schaller
        Dec 13 '18 at 2:09













        @JeffSchaller I noticed that :-)

        – Stephen Harris
        Dec 13 '18 at 2:10







        @JeffSchaller I noticed that :-)

        – Stephen Harris
        Dec 13 '18 at 2:10






        1




        1





        @JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean the for loop won't demonstrate the solution working. I'll edit the answer to make that clear.

        – Stephen Harris
        Dec 13 '18 at 2:14







        @JeffSchaller I rolled back your edit; I specifically put a 3 second pause in as an example; a 300 second (5minute) sleep would mean the for loop won't demonstrate the solution working. I'll edit the answer to make that clear.

        – Stephen Harris
        Dec 13 '18 at 2:14















        6














        Sure:



        trap 'unset x; trap - USR1' USR1; { sleep 5m; kill -USR1 $$; } &


        This sets a trap on the USR1 signal, then (cheating with a semicolon to put it on one line) groups together the sleep and kill commands into a background job. When that job completes, it will send the USR1 signal to the current shell, which will execute the trap. The trap unsets x and then clears the trap.



        No functions!






        share|improve this answer






























          6














          Sure:



          trap 'unset x; trap - USR1' USR1; { sleep 5m; kill -USR1 $$; } &


          This sets a trap on the USR1 signal, then (cheating with a semicolon to put it on one line) groups together the sleep and kill commands into a background job. When that job completes, it will send the USR1 signal to the current shell, which will execute the trap. The trap unsets x and then clears the trap.



          No functions!






          share|improve this answer




























            6












            6








            6







            Sure:



            trap 'unset x; trap - USR1' USR1; { sleep 5m; kill -USR1 $$; } &


            This sets a trap on the USR1 signal, then (cheating with a semicolon to put it on one line) groups together the sleep and kill commands into a background job. When that job completes, it will send the USR1 signal to the current shell, which will execute the trap. The trap unsets x and then clears the trap.



            No functions!






            share|improve this answer















            Sure:



            trap 'unset x; trap - USR1' USR1; { sleep 5m; kill -USR1 $$; } &


            This sets a trap on the USR1 signal, then (cheating with a semicolon to put it on one line) groups together the sleep and kill commands into a background job. When that job completes, it will send the USR1 signal to the current shell, which will execute the trap. The trap unsets x and then clears the trap.



            No functions!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 13 '18 at 2:27

























            answered Dec 13 '18 at 2:02









            Jeff SchallerJeff Schaller

            39.3k1054125




            39.3k1054125























                1














                With your revised question, based around the command line and not a script, your simplest solution is to use a subshell to do your work.



                eg



                $ bash
                $ x=100
                $ echo $x
                100
                $ exit
                $ echo $x

                $


                All the variables you set between running bash and exiting that shell will be forgotten.



                This is very close to how the wget ... | bash part works as well, except you can cut'n'paste/type commands.






                share|improve this answer




























                  1














                  With your revised question, based around the command line and not a script, your simplest solution is to use a subshell to do your work.



                  eg



                  $ bash
                  $ x=100
                  $ echo $x
                  100
                  $ exit
                  $ echo $x

                  $


                  All the variables you set between running bash and exiting that shell will be forgotten.



                  This is very close to how the wget ... | bash part works as well, except you can cut'n'paste/type commands.






                  share|improve this answer


























                    1












                    1








                    1







                    With your revised question, based around the command line and not a script, your simplest solution is to use a subshell to do your work.



                    eg



                    $ bash
                    $ x=100
                    $ echo $x
                    100
                    $ exit
                    $ echo $x

                    $


                    All the variables you set between running bash and exiting that shell will be forgotten.



                    This is very close to how the wget ... | bash part works as well, except you can cut'n'paste/type commands.






                    share|improve this answer













                    With your revised question, based around the command line and not a script, your simplest solution is to use a subshell to do your work.



                    eg



                    $ bash
                    $ x=100
                    $ echo $x
                    100
                    $ exit
                    $ echo $x

                    $


                    All the variables you set between running bash and exiting that shell will be forgotten.



                    This is very close to how the wget ... | bash part works as well, except you can cut'n'paste/type commands.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 13 '18 at 17:40









                    Stephen HarrisStephen Harris

                    25.4k24477




                    25.4k24477























                        0














                        Actually the simplest way might be to just use a compound command with a variable declaration at the start.



                        If you declare a variable at the start of a command line, the variable is temporarily defined only for the scope of that command.



                        madumlao@lezard:~$ x=foo
                        madumlao@lezard:~$ x=y bash -c 'echo $x'
                        y
                        madumlao@lezard:~$ echo $x
                        foo


                        As you can see, the line with a variable declaration only sets the variable in the environment for that line only.



                        Note that the following will not work:



                        madumlao@lezard:~$ x=foo
                        madumlao@lezard:~$ x=y echo $x

                        madumlao@lezard:~$ echo $x
                        foo


                        The $x in the second line is parsed during command-line parsing, before the value is actually assigned, so it is unexpectedly blank!






                        share|improve this answer




























                          0














                          Actually the simplest way might be to just use a compound command with a variable declaration at the start.



                          If you declare a variable at the start of a command line, the variable is temporarily defined only for the scope of that command.



                          madumlao@lezard:~$ x=foo
                          madumlao@lezard:~$ x=y bash -c 'echo $x'
                          y
                          madumlao@lezard:~$ echo $x
                          foo


                          As you can see, the line with a variable declaration only sets the variable in the environment for that line only.



                          Note that the following will not work:



                          madumlao@lezard:~$ x=foo
                          madumlao@lezard:~$ x=y echo $x

                          madumlao@lezard:~$ echo $x
                          foo


                          The $x in the second line is parsed during command-line parsing, before the value is actually assigned, so it is unexpectedly blank!






                          share|improve this answer


























                            0












                            0








                            0







                            Actually the simplest way might be to just use a compound command with a variable declaration at the start.



                            If you declare a variable at the start of a command line, the variable is temporarily defined only for the scope of that command.



                            madumlao@lezard:~$ x=foo
                            madumlao@lezard:~$ x=y bash -c 'echo $x'
                            y
                            madumlao@lezard:~$ echo $x
                            foo


                            As you can see, the line with a variable declaration only sets the variable in the environment for that line only.



                            Note that the following will not work:



                            madumlao@lezard:~$ x=foo
                            madumlao@lezard:~$ x=y echo $x

                            madumlao@lezard:~$ echo $x
                            foo


                            The $x in the second line is parsed during command-line parsing, before the value is actually assigned, so it is unexpectedly blank!






                            share|improve this answer













                            Actually the simplest way might be to just use a compound command with a variable declaration at the start.



                            If you declare a variable at the start of a command line, the variable is temporarily defined only for the scope of that command.



                            madumlao@lezard:~$ x=foo
                            madumlao@lezard:~$ x=y bash -c 'echo $x'
                            y
                            madumlao@lezard:~$ echo $x
                            foo


                            As you can see, the line with a variable declaration only sets the variable in the environment for that line only.



                            Note that the following will not work:



                            madumlao@lezard:~$ x=foo
                            madumlao@lezard:~$ x=y echo $x

                            madumlao@lezard:~$ echo $x
                            foo


                            The $x in the second line is parsed during command-line parsing, before the value is actually assigned, so it is unexpectedly blank!







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 13 '18 at 17:11









                            madumlaomadumlao

                            1,14166




                            1,14166






























                                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%2f487683%2ftemporarily-declare-a-variable-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

                                Сан-Квентин

                                8-я гвардейская общевойсковая армия

                                Алькесар