Defining several variables at once (copy-paste-execute in shell)How to print shell variables and values to be...

How to find out the average duration of the peer-review process for a given journal?

Are the players on the same team as the DM?

Is for(( ... )){ ... ;} a valid shell syntax? In which shells?

Are modern clipless shoes and pedals that much better than toe clips and straps?

Does norwegian.no airline overbook flights?

“T” in subscript in formulas

French abbreviation for comparing two items ("vs")

Nothing like a good ol' game of ModTen

Is gzip atomic?

Is there any music source code for sound chips?

Does travel insurance for short flight delays exist?

Is “I am getting married with my sister” ambiguous?

Why do banks “park” their money at the European Central Bank?

What to say to a student who has failed?

How much authority do teachers get from *In Loco Parentis*?

Why are non-collision-resistant hash functions considered insecure for signing self-generated information

How would you identify when an object in a Lissajous orbit needs station keeping?

What are some interesting features that are common cross-linguistically but don't exist in English?

Is a player able to change alignment midway through an adventure?

Are there any elected officials in the U.S. who are not legislators, judges, or constitutional officers?

Why isn't "I've" a proper response?

What is the difference between Major and Minor Bug?

Why did Khan ask Admiral James T. Kirk about Project Genesis?

Is it possible to perform a regression where you have an unknown / unknowable feature variable?



Defining several variables at once (copy-paste-execute in shell)


How to print shell variables and values to be able to copy/paste them?When should one use $( ) in defining variablesSed command usage without defining variables in shell scriptbash- defining variables with VAR=${[number]:-default}Execution of a remote Bash script in GitHub fails with various methods - Maybe due to Windows10Shell Script : Variables correct formatUsing a bash array in an awk and also quoted variable: conflicting syntax issueRunning script line by line automatically yet being asked before each line from second line onwards






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I have a list of variables I copy-paste one by one from a file to my shell and hit Enter to execute. For example:



var1="myvar1"
var2="myvar2"


Because in practice there are more variables, instead copy-pasting them and execute them one by one I desire to do all in one operation to save time.

I tried copy-pasting:



(
var1="myvar1"
var2="myvar2"
)


echo $var1 outputs an empty line instead date in relevant format.



How could I execute a few variables in one operation?










share|improve this question

































    0















    I have a list of variables I copy-paste one by one from a file to my shell and hit Enter to execute. For example:



    var1="myvar1"
    var2="myvar2"


    Because in practice there are more variables, instead copy-pasting them and execute them one by one I desire to do all in one operation to save time.

    I tried copy-pasting:



    (
    var1="myvar1"
    var2="myvar2"
    )


    echo $var1 outputs an empty line instead date in relevant format.



    How could I execute a few variables in one operation?










    share|improve this question





























      0












      0








      0








      I have a list of variables I copy-paste one by one from a file to my shell and hit Enter to execute. For example:



      var1="myvar1"
      var2="myvar2"


      Because in practice there are more variables, instead copy-pasting them and execute them one by one I desire to do all in one operation to save time.

      I tried copy-pasting:



      (
      var1="myvar1"
      var2="myvar2"
      )


      echo $var1 outputs an empty line instead date in relevant format.



      How could I execute a few variables in one operation?










      share|improve this question
















      I have a list of variables I copy-paste one by one from a file to my shell and hit Enter to execute. For example:



      var1="myvar1"
      var2="myvar2"


      Because in practice there are more variables, instead copy-pasting them and execute them one by one I desire to do all in one operation to save time.

      I tried copy-pasting:



      (
      var1="myvar1"
      var2="myvar2"
      )


      echo $var1 outputs an empty line instead date in relevant format.



      How could I execute a few variables in one operation?







      shell-script variable syntax






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday







      JohnDoea

















      asked yesterday









      JohnDoeaJohnDoea

      721 gold badge11 silver badges48 bronze badges




      721 gold badge11 silver badges48 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          3















          You don't have copy them one by one, you can paste all the lines together and newlines will work as Enter.



          The reason that



          (
          var1="myvar1"
          var2="myvar2"
          )


          doesn't work is that because it's executed in a subshell. It would work if you printed contents of the variable before the final
          ):



          (
          var1="myvar1"
          var2="myvar2"
          echo $var2
          )


          It's explained in Commpound Commands section in man bash:




          Compound Commands



          A compound command is one of the following. In most cases a list in a
          command's description may be sepa- rated from the rest of the command
          by one or more newlines, and may be followed by a newline in place of
          a semicolon.



          (list) list is executed in a subshell environment (see COMMAND
          EXECUTION ENVIRONMENT below). Variable assignments and builtin
          commands that affect the shell's environment do not remain in effect
          after the command completes. The return status is the exit status
          of list.




          Later on it also says:




          { list; }
          list is simply executed in the current shell environment.
          list must be terminated with a newline or semicolon. This is known
          as a group command. The return status is the exit status of list.
          Note that unlike the metacharacters ( and ), { and } are reserved
          words and must occur where a reserved word is permitted to be
          recognized. Since they do not cause a word break, they must be
          separated from list by whitespace or another shell metacharacter.




          So that would work as commands would not be executed in a subshell but
          in a current shell environment:



          {
          var1="myvar1"
          var2="myvar2";
          }


          I see you're asking a lot of simple questions about Bash. Consider
          reading some Bash tutorials, man bash and learn about common Bash
          pitfalls.






          share|improve this answer




























          • Some of the facts you mention I learned a few years back but some I forgot because I am not a professional sysadmin or doing sysadministration on a daily basis... Sometimes I prefer to ask to have direct documentation for me and others. I left my home country and left my books behind and not in a comfortable situation to read huge documentation. I had a Bash book from 2005 I used to read in train; I should buy the new edition in the right time for me...

            – JohnDoea
            yesterday



















          0















          The parenthesis you used causes the enclosed commands to be executed by a subshell; hence once that shell exits, de variables are still not set in the current shell.



          I'm at a loss why you think those parenthesis would set those variables "in one go", or why it would be necessary to them them "in one go"; however you could place those assignments on one line such as:



          var1="myvar1" var2="myvar2"


          Of course there's no reason not to copy and paste those lines all together in one paste, as Arkadiusz Drabczyk commented, if that's easier and all that you want to accomplish.






          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%2f536827%2fdefining-several-variables-at-once-copy-paste-execute-in-shell%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









            3















            You don't have copy them one by one, you can paste all the lines together and newlines will work as Enter.



            The reason that



            (
            var1="myvar1"
            var2="myvar2"
            )


            doesn't work is that because it's executed in a subshell. It would work if you printed contents of the variable before the final
            ):



            (
            var1="myvar1"
            var2="myvar2"
            echo $var2
            )


            It's explained in Commpound Commands section in man bash:




            Compound Commands



            A compound command is one of the following. In most cases a list in a
            command's description may be sepa- rated from the rest of the command
            by one or more newlines, and may be followed by a newline in place of
            a semicolon.



            (list) list is executed in a subshell environment (see COMMAND
            EXECUTION ENVIRONMENT below). Variable assignments and builtin
            commands that affect the shell's environment do not remain in effect
            after the command completes. The return status is the exit status
            of list.




            Later on it also says:




            { list; }
            list is simply executed in the current shell environment.
            list must be terminated with a newline or semicolon. This is known
            as a group command. The return status is the exit status of list.
            Note that unlike the metacharacters ( and ), { and } are reserved
            words and must occur where a reserved word is permitted to be
            recognized. Since they do not cause a word break, they must be
            separated from list by whitespace or another shell metacharacter.




            So that would work as commands would not be executed in a subshell but
            in a current shell environment:



            {
            var1="myvar1"
            var2="myvar2";
            }


            I see you're asking a lot of simple questions about Bash. Consider
            reading some Bash tutorials, man bash and learn about common Bash
            pitfalls.






            share|improve this answer




























            • Some of the facts you mention I learned a few years back but some I forgot because I am not a professional sysadmin or doing sysadministration on a daily basis... Sometimes I prefer to ask to have direct documentation for me and others. I left my home country and left my books behind and not in a comfortable situation to read huge documentation. I had a Bash book from 2005 I used to read in train; I should buy the new edition in the right time for me...

              – JohnDoea
              yesterday
















            3















            You don't have copy them one by one, you can paste all the lines together and newlines will work as Enter.



            The reason that



            (
            var1="myvar1"
            var2="myvar2"
            )


            doesn't work is that because it's executed in a subshell. It would work if you printed contents of the variable before the final
            ):



            (
            var1="myvar1"
            var2="myvar2"
            echo $var2
            )


            It's explained in Commpound Commands section in man bash:




            Compound Commands



            A compound command is one of the following. In most cases a list in a
            command's description may be sepa- rated from the rest of the command
            by one or more newlines, and may be followed by a newline in place of
            a semicolon.



            (list) list is executed in a subshell environment (see COMMAND
            EXECUTION ENVIRONMENT below). Variable assignments and builtin
            commands that affect the shell's environment do not remain in effect
            after the command completes. The return status is the exit status
            of list.




            Later on it also says:




            { list; }
            list is simply executed in the current shell environment.
            list must be terminated with a newline or semicolon. This is known
            as a group command. The return status is the exit status of list.
            Note that unlike the metacharacters ( and ), { and } are reserved
            words and must occur where a reserved word is permitted to be
            recognized. Since they do not cause a word break, they must be
            separated from list by whitespace or another shell metacharacter.




            So that would work as commands would not be executed in a subshell but
            in a current shell environment:



            {
            var1="myvar1"
            var2="myvar2";
            }


            I see you're asking a lot of simple questions about Bash. Consider
            reading some Bash tutorials, man bash and learn about common Bash
            pitfalls.






            share|improve this answer




























            • Some of the facts you mention I learned a few years back but some I forgot because I am not a professional sysadmin or doing sysadministration on a daily basis... Sometimes I prefer to ask to have direct documentation for me and others. I left my home country and left my books behind and not in a comfortable situation to read huge documentation. I had a Bash book from 2005 I used to read in train; I should buy the new edition in the right time for me...

              – JohnDoea
              yesterday














            3














            3










            3









            You don't have copy them one by one, you can paste all the lines together and newlines will work as Enter.



            The reason that



            (
            var1="myvar1"
            var2="myvar2"
            )


            doesn't work is that because it's executed in a subshell. It would work if you printed contents of the variable before the final
            ):



            (
            var1="myvar1"
            var2="myvar2"
            echo $var2
            )


            It's explained in Commpound Commands section in man bash:




            Compound Commands



            A compound command is one of the following. In most cases a list in a
            command's description may be sepa- rated from the rest of the command
            by one or more newlines, and may be followed by a newline in place of
            a semicolon.



            (list) list is executed in a subshell environment (see COMMAND
            EXECUTION ENVIRONMENT below). Variable assignments and builtin
            commands that affect the shell's environment do not remain in effect
            after the command completes. The return status is the exit status
            of list.




            Later on it also says:




            { list; }
            list is simply executed in the current shell environment.
            list must be terminated with a newline or semicolon. This is known
            as a group command. The return status is the exit status of list.
            Note that unlike the metacharacters ( and ), { and } are reserved
            words and must occur where a reserved word is permitted to be
            recognized. Since they do not cause a word break, they must be
            separated from list by whitespace or another shell metacharacter.




            So that would work as commands would not be executed in a subshell but
            in a current shell environment:



            {
            var1="myvar1"
            var2="myvar2";
            }


            I see you're asking a lot of simple questions about Bash. Consider
            reading some Bash tutorials, man bash and learn about common Bash
            pitfalls.






            share|improve this answer















            You don't have copy them one by one, you can paste all the lines together and newlines will work as Enter.



            The reason that



            (
            var1="myvar1"
            var2="myvar2"
            )


            doesn't work is that because it's executed in a subshell. It would work if you printed contents of the variable before the final
            ):



            (
            var1="myvar1"
            var2="myvar2"
            echo $var2
            )


            It's explained in Commpound Commands section in man bash:




            Compound Commands



            A compound command is one of the following. In most cases a list in a
            command's description may be sepa- rated from the rest of the command
            by one or more newlines, and may be followed by a newline in place of
            a semicolon.



            (list) list is executed in a subshell environment (see COMMAND
            EXECUTION ENVIRONMENT below). Variable assignments and builtin
            commands that affect the shell's environment do not remain in effect
            after the command completes. The return status is the exit status
            of list.




            Later on it also says:




            { list; }
            list is simply executed in the current shell environment.
            list must be terminated with a newline or semicolon. This is known
            as a group command. The return status is the exit status of list.
            Note that unlike the metacharacters ( and ), { and } are reserved
            words and must occur where a reserved word is permitted to be
            recognized. Since they do not cause a word break, they must be
            separated from list by whitespace or another shell metacharacter.




            So that would work as commands would not be executed in a subshell but
            in a current shell environment:



            {
            var1="myvar1"
            var2="myvar2";
            }


            I see you're asking a lot of simple questions about Bash. Consider
            reading some Bash tutorials, man bash and learn about common Bash
            pitfalls.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday

























            answered yesterday









            Arkadiusz DrabczykArkadiusz Drabczyk

            9,0883 gold badges20 silver badges36 bronze badges




            9,0883 gold badges20 silver badges36 bronze badges
















            • Some of the facts you mention I learned a few years back but some I forgot because I am not a professional sysadmin or doing sysadministration on a daily basis... Sometimes I prefer to ask to have direct documentation for me and others. I left my home country and left my books behind and not in a comfortable situation to read huge documentation. I had a Bash book from 2005 I used to read in train; I should buy the new edition in the right time for me...

              – JohnDoea
              yesterday



















            • Some of the facts you mention I learned a few years back but some I forgot because I am not a professional sysadmin or doing sysadministration on a daily basis... Sometimes I prefer to ask to have direct documentation for me and others. I left my home country and left my books behind and not in a comfortable situation to read huge documentation. I had a Bash book from 2005 I used to read in train; I should buy the new edition in the right time for me...

              – JohnDoea
              yesterday

















            Some of the facts you mention I learned a few years back but some I forgot because I am not a professional sysadmin or doing sysadministration on a daily basis... Sometimes I prefer to ask to have direct documentation for me and others. I left my home country and left my books behind and not in a comfortable situation to read huge documentation. I had a Bash book from 2005 I used to read in train; I should buy the new edition in the right time for me...

            – JohnDoea
            yesterday





            Some of the facts you mention I learned a few years back but some I forgot because I am not a professional sysadmin or doing sysadministration on a daily basis... Sometimes I prefer to ask to have direct documentation for me and others. I left my home country and left my books behind and not in a comfortable situation to read huge documentation. I had a Bash book from 2005 I used to read in train; I should buy the new edition in the right time for me...

            – JohnDoea
            yesterday













            0















            The parenthesis you used causes the enclosed commands to be executed by a subshell; hence once that shell exits, de variables are still not set in the current shell.



            I'm at a loss why you think those parenthesis would set those variables "in one go", or why it would be necessary to them them "in one go"; however you could place those assignments on one line such as:



            var1="myvar1" var2="myvar2"


            Of course there's no reason not to copy and paste those lines all together in one paste, as Arkadiusz Drabczyk commented, if that's easier and all that you want to accomplish.






            share|improve this answer
































              0















              The parenthesis you used causes the enclosed commands to be executed by a subshell; hence once that shell exits, de variables are still not set in the current shell.



              I'm at a loss why you think those parenthesis would set those variables "in one go", or why it would be necessary to them them "in one go"; however you could place those assignments on one line such as:



              var1="myvar1" var2="myvar2"


              Of course there's no reason not to copy and paste those lines all together in one paste, as Arkadiusz Drabczyk commented, if that's easier and all that you want to accomplish.






              share|improve this answer






























                0














                0










                0









                The parenthesis you used causes the enclosed commands to be executed by a subshell; hence once that shell exits, de variables are still not set in the current shell.



                I'm at a loss why you think those parenthesis would set those variables "in one go", or why it would be necessary to them them "in one go"; however you could place those assignments on one line such as:



                var1="myvar1" var2="myvar2"


                Of course there's no reason not to copy and paste those lines all together in one paste, as Arkadiusz Drabczyk commented, if that's easier and all that you want to accomplish.






                share|improve this answer















                The parenthesis you used causes the enclosed commands to be executed by a subshell; hence once that shell exits, de variables are still not set in the current shell.



                I'm at a loss why you think those parenthesis would set those variables "in one go", or why it would be necessary to them them "in one go"; however you could place those assignments on one line such as:



                var1="myvar1" var2="myvar2"


                Of course there's no reason not to copy and paste those lines all together in one paste, as Arkadiusz Drabczyk commented, if that's easier and all that you want to accomplish.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited yesterday









                ilkkachu

                67.3k10 gold badges112 silver badges193 bronze badges




                67.3k10 gold badges112 silver badges193 bronze badges










                answered yesterday









                wurtelwurtel

                11.8k1 gold badge16 silver badges29 bronze badges




                11.8k1 gold badge16 silver badges29 bronze badges

































                    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%2f536827%2fdefining-several-variables-at-once-copy-paste-execute-in-shell%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

                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                    Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m