passing export command to function in shell script

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











up vote
1
down vote

favorite












#!/bin/sh

execute_cmd()

$($@)


execute_cmd export MY_VAR=my_val
echo $MY_VAR


Since $() executes in a sub-shell, $MY_VAR isn't set properly in the shell the script is running.



My question, how can I pass the export command to a function and execute it in the current shell the script is running in?










share|improve this question



























    up vote
    1
    down vote

    favorite












    #!/bin/sh

    execute_cmd()

    $($@)


    execute_cmd export MY_VAR=my_val
    echo $MY_VAR


    Since $() executes in a sub-shell, $MY_VAR isn't set properly in the shell the script is running.



    My question, how can I pass the export command to a function and execute it in the current shell the script is running in?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      #!/bin/sh

      execute_cmd()

      $($@)


      execute_cmd export MY_VAR=my_val
      echo $MY_VAR


      Since $() executes in a sub-shell, $MY_VAR isn't set properly in the shell the script is running.



      My question, how can I pass the export command to a function and execute it in the current shell the script is running in?










      share|improve this question















      #!/bin/sh

      execute_cmd()

      $($@)


      execute_cmd export MY_VAR=my_val
      echo $MY_VAR


      Since $() executes in a sub-shell, $MY_VAR isn't set properly in the shell the script is running.



      My question, how can I pass the export command to a function and execute it in the current shell the script is running in?







      shell function






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 28 '17 at 23:32









      Gilles

      513k12010191549




      513k12010191549










      asked Aug 28 '17 at 16:51









      user3763392

      82




      82




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Use eval instead of $():



          execute_cmd()

          eval $@



          From bash manual:




          eval [arguments]




          The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.





          Other shells usually have eval built-in with similar semantics.






          share|improve this answer






















          • Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g. eval $@ 2>&1 | tee -a $logfile
            – user3763392
            Aug 28 '17 at 17:52











          • Usual output redirection: eval echo "Hello World!" > log.txt, or execute_cmd echo "Hello World" > log.txt. Note the latter redirects the output of execute_cmd instead of passing those characters to the function. To redirect in execute_cmd pass the redirection as string: execute_cmd 'echo "Hello World" > log.txt'.
            – sebasth
            Aug 28 '17 at 17:59







          • 1




            eval $@ doesn't make sense. It should probably be either "$@" or eval "$1", or possibly eval "$@" (which makes execute_cmd redundant since it's equivalent to eval).
            – Gilles
            Aug 28 '17 at 22:40

















          up vote
          2
          down vote













          It isn't clear what you want to do. $($@) doesn't make sense, but what do you want to do instead?



          What $($@) does:



          • Take the arguments of the function. This is a list of strings.

          • Split each piece further where they contain whitespace¹.

          • Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.

          • Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.

          • Take the output of the command and split it where it contains whitespace.

          • Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.

          • Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.

          If that sounds complicated, that's because it is.



          If you want execute_cmd export MY_VAR=my_val to execute export MY_VAR=my_val, why are you even bothering with execute_cmd?



          There are two ways this could be interpreted sensibly.




          1. You want to pass a command with parameters to a function. A command with parameters is a list of strings, the first being a function name, shell builtin or executable file. Then the syntax to invoke this command on the supplied parameters is "$@".



            execute_cmd () 
            "$@"

            execute_cmd export MY_VAR=my_val


            The double quotes avoid the splitting and wildcard expansion steps I mentioned above. Always use double quotes around variable substitutions.




          2. You want to run a shell snippet. A shell snippet is a single string, to be passed as a single argument. To run a string containing shell code, use the eval builtin.



            execute_cmd () 
            eval "$1"

            execute_cmd 'export MY_VAR=my_val'


          ¹ Assuming the default IFS. If you know about that you don't need to read this paragraph.






          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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f388886%2fpassing-export-command-to-function-in-shell-script%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Use eval instead of $():



            execute_cmd()

            eval $@



            From bash manual:




            eval [arguments]




            The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.





            Other shells usually have eval built-in with similar semantics.






            share|improve this answer






















            • Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g. eval $@ 2>&1 | tee -a $logfile
              – user3763392
              Aug 28 '17 at 17:52











            • Usual output redirection: eval echo "Hello World!" > log.txt, or execute_cmd echo "Hello World" > log.txt. Note the latter redirects the output of execute_cmd instead of passing those characters to the function. To redirect in execute_cmd pass the redirection as string: execute_cmd 'echo "Hello World" > log.txt'.
              – sebasth
              Aug 28 '17 at 17:59







            • 1




              eval $@ doesn't make sense. It should probably be either "$@" or eval "$1", or possibly eval "$@" (which makes execute_cmd redundant since it's equivalent to eval).
              – Gilles
              Aug 28 '17 at 22:40














            up vote
            0
            down vote



            accepted










            Use eval instead of $():



            execute_cmd()

            eval $@



            From bash manual:




            eval [arguments]




            The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.





            Other shells usually have eval built-in with similar semantics.






            share|improve this answer






















            • Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g. eval $@ 2>&1 | tee -a $logfile
              – user3763392
              Aug 28 '17 at 17:52











            • Usual output redirection: eval echo "Hello World!" > log.txt, or execute_cmd echo "Hello World" > log.txt. Note the latter redirects the output of execute_cmd instead of passing those characters to the function. To redirect in execute_cmd pass the redirection as string: execute_cmd 'echo "Hello World" > log.txt'.
              – sebasth
              Aug 28 '17 at 17:59







            • 1




              eval $@ doesn't make sense. It should probably be either "$@" or eval "$1", or possibly eval "$@" (which makes execute_cmd redundant since it's equivalent to eval).
              – Gilles
              Aug 28 '17 at 22:40












            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            Use eval instead of $():



            execute_cmd()

            eval $@



            From bash manual:




            eval [arguments]




            The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.





            Other shells usually have eval built-in with similar semantics.






            share|improve this answer














            Use eval instead of $():



            execute_cmd()

            eval $@



            From bash manual:




            eval [arguments]




            The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.





            Other shells usually have eval built-in with similar semantics.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 10 mins ago

























            answered Aug 28 '17 at 16:58









            sebasth

            7,44631745




            7,44631745











            • Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g. eval $@ 2>&1 | tee -a $logfile
              – user3763392
              Aug 28 '17 at 17:52











            • Usual output redirection: eval echo "Hello World!" > log.txt, or execute_cmd echo "Hello World" > log.txt. Note the latter redirects the output of execute_cmd instead of passing those characters to the function. To redirect in execute_cmd pass the redirection as string: execute_cmd 'echo "Hello World" > log.txt'.
              – sebasth
              Aug 28 '17 at 17:59







            • 1




              eval $@ doesn't make sense. It should probably be either "$@" or eval "$1", or possibly eval "$@" (which makes execute_cmd redundant since it's equivalent to eval).
              – Gilles
              Aug 28 '17 at 22:40
















            • Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g. eval $@ 2>&1 | tee -a $logfile
              – user3763392
              Aug 28 '17 at 17:52











            • Usual output redirection: eval echo "Hello World!" > log.txt, or execute_cmd echo "Hello World" > log.txt. Note the latter redirects the output of execute_cmd instead of passing those characters to the function. To redirect in execute_cmd pass the redirection as string: execute_cmd 'echo "Hello World" > log.txt'.
              – sebasth
              Aug 28 '17 at 17:59







            • 1




              eval $@ doesn't make sense. It should probably be either "$@" or eval "$1", or possibly eval "$@" (which makes execute_cmd redundant since it's equivalent to eval).
              – Gilles
              Aug 28 '17 at 22:40















            Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g. eval $@ 2>&1 | tee -a $logfile
            – user3763392
            Aug 28 '17 at 17:52





            Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g. eval $@ 2>&1 | tee -a $logfile
            – user3763392
            Aug 28 '17 at 17:52













            Usual output redirection: eval echo "Hello World!" > log.txt, or execute_cmd echo "Hello World" > log.txt. Note the latter redirects the output of execute_cmd instead of passing those characters to the function. To redirect in execute_cmd pass the redirection as string: execute_cmd 'echo "Hello World" > log.txt'.
            – sebasth
            Aug 28 '17 at 17:59





            Usual output redirection: eval echo "Hello World!" > log.txt, or execute_cmd echo "Hello World" > log.txt. Note the latter redirects the output of execute_cmd instead of passing those characters to the function. To redirect in execute_cmd pass the redirection as string: execute_cmd 'echo "Hello World" > log.txt'.
            – sebasth
            Aug 28 '17 at 17:59





            1




            1




            eval $@ doesn't make sense. It should probably be either "$@" or eval "$1", or possibly eval "$@" (which makes execute_cmd redundant since it's equivalent to eval).
            – Gilles
            Aug 28 '17 at 22:40




            eval $@ doesn't make sense. It should probably be either "$@" or eval "$1", or possibly eval "$@" (which makes execute_cmd redundant since it's equivalent to eval).
            – Gilles
            Aug 28 '17 at 22:40












            up vote
            2
            down vote













            It isn't clear what you want to do. $($@) doesn't make sense, but what do you want to do instead?



            What $($@) does:



            • Take the arguments of the function. This is a list of strings.

            • Split each piece further where they contain whitespace¹.

            • Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.

            • Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.

            • Take the output of the command and split it where it contains whitespace.

            • Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.

            • Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.

            If that sounds complicated, that's because it is.



            If you want execute_cmd export MY_VAR=my_val to execute export MY_VAR=my_val, why are you even bothering with execute_cmd?



            There are two ways this could be interpreted sensibly.




            1. You want to pass a command with parameters to a function. A command with parameters is a list of strings, the first being a function name, shell builtin or executable file. Then the syntax to invoke this command on the supplied parameters is "$@".



              execute_cmd () 
              "$@"

              execute_cmd export MY_VAR=my_val


              The double quotes avoid the splitting and wildcard expansion steps I mentioned above. Always use double quotes around variable substitutions.




            2. You want to run a shell snippet. A shell snippet is a single string, to be passed as a single argument. To run a string containing shell code, use the eval builtin.



              execute_cmd () 
              eval "$1"

              execute_cmd 'export MY_VAR=my_val'


            ¹ Assuming the default IFS. If you know about that you don't need to read this paragraph.






            share|improve this answer
























              up vote
              2
              down vote













              It isn't clear what you want to do. $($@) doesn't make sense, but what do you want to do instead?



              What $($@) does:



              • Take the arguments of the function. This is a list of strings.

              • Split each piece further where they contain whitespace¹.

              • Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.

              • Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.

              • Take the output of the command and split it where it contains whitespace.

              • Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.

              • Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.

              If that sounds complicated, that's because it is.



              If you want execute_cmd export MY_VAR=my_val to execute export MY_VAR=my_val, why are you even bothering with execute_cmd?



              There are two ways this could be interpreted sensibly.




              1. You want to pass a command with parameters to a function. A command with parameters is a list of strings, the first being a function name, shell builtin or executable file. Then the syntax to invoke this command on the supplied parameters is "$@".



                execute_cmd () 
                "$@"

                execute_cmd export MY_VAR=my_val


                The double quotes avoid the splitting and wildcard expansion steps I mentioned above. Always use double quotes around variable substitutions.




              2. You want to run a shell snippet. A shell snippet is a single string, to be passed as a single argument. To run a string containing shell code, use the eval builtin.



                execute_cmd () 
                eval "$1"

                execute_cmd 'export MY_VAR=my_val'


              ¹ Assuming the default IFS. If you know about that you don't need to read this paragraph.






              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                It isn't clear what you want to do. $($@) doesn't make sense, but what do you want to do instead?



                What $($@) does:



                • Take the arguments of the function. This is a list of strings.

                • Split each piece further where they contain whitespace¹.

                • Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.

                • Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.

                • Take the output of the command and split it where it contains whitespace.

                • Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.

                • Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.

                If that sounds complicated, that's because it is.



                If you want execute_cmd export MY_VAR=my_val to execute export MY_VAR=my_val, why are you even bothering with execute_cmd?



                There are two ways this could be interpreted sensibly.




                1. You want to pass a command with parameters to a function. A command with parameters is a list of strings, the first being a function name, shell builtin or executable file. Then the syntax to invoke this command on the supplied parameters is "$@".



                  execute_cmd () 
                  "$@"

                  execute_cmd export MY_VAR=my_val


                  The double quotes avoid the splitting and wildcard expansion steps I mentioned above. Always use double quotes around variable substitutions.




                2. You want to run a shell snippet. A shell snippet is a single string, to be passed as a single argument. To run a string containing shell code, use the eval builtin.



                  execute_cmd () 
                  eval "$1"

                  execute_cmd 'export MY_VAR=my_val'


                ¹ Assuming the default IFS. If you know about that you don't need to read this paragraph.






                share|improve this answer












                It isn't clear what you want to do. $($@) doesn't make sense, but what do you want to do instead?



                What $($@) does:



                • Take the arguments of the function. This is a list of strings.

                • Split each piece further where they contain whitespace¹.

                • Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.

                • Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.

                • Take the output of the command and split it where it contains whitespace.

                • Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.

                • Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.

                If that sounds complicated, that's because it is.



                If you want execute_cmd export MY_VAR=my_val to execute export MY_VAR=my_val, why are you even bothering with execute_cmd?



                There are two ways this could be interpreted sensibly.




                1. You want to pass a command with parameters to a function. A command with parameters is a list of strings, the first being a function name, shell builtin or executable file. Then the syntax to invoke this command on the supplied parameters is "$@".



                  execute_cmd () 
                  "$@"

                  execute_cmd export MY_VAR=my_val


                  The double quotes avoid the splitting and wildcard expansion steps I mentioned above. Always use double quotes around variable substitutions.




                2. You want to run a shell snippet. A shell snippet is a single string, to be passed as a single argument. To run a string containing shell code, use the eval builtin.



                  execute_cmd () 
                  eval "$1"

                  execute_cmd 'export MY_VAR=my_val'


                ¹ Assuming the default IFS. If you know about that you don't need to read this paragraph.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 28 '17 at 23:32









                Gilles

                513k12010191549




                513k12010191549



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f388886%2fpassing-export-command-to-function-in-shell-script%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

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

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?