Include a bash function into the parent script

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











up vote
1
down vote

favorite












I can to define function in bash and use it:



foo() echo $1; 
foo test


But if I want to collect my functions in one bash script all its unavailable:



init.bash



#!/bin/bash
foo() echo $1;
export -f foo # This not helps


Using:



./init.bash && foo test # error here


Is there any way to export script's functions to parent scope?

Without writing to .bashrc, it's too global

Same as .bashrc but for current bash instance only...










share|improve this question



























    up vote
    1
    down vote

    favorite












    I can to define function in bash and use it:



    foo() echo $1; 
    foo test


    But if I want to collect my functions in one bash script all its unavailable:



    init.bash



    #!/bin/bash
    foo() echo $1;
    export -f foo # This not helps


    Using:



    ./init.bash && foo test # error here


    Is there any way to export script's functions to parent scope?

    Without writing to .bashrc, it's too global

    Same as .bashrc but for current bash instance only...










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I can to define function in bash and use it:



      foo() echo $1; 
      foo test


      But if I want to collect my functions in one bash script all its unavailable:



      init.bash



      #!/bin/bash
      foo() echo $1;
      export -f foo # This not helps


      Using:



      ./init.bash && foo test # error here


      Is there any way to export script's functions to parent scope?

      Without writing to .bashrc, it's too global

      Same as .bashrc but for current bash instance only...










      share|improve this question















      I can to define function in bash and use it:



      foo() echo $1; 
      foo test


      But if I want to collect my functions in one bash script all its unavailable:



      init.bash



      #!/bin/bash
      foo() echo $1;
      export -f foo # This not helps


      Using:



      ./init.bash && foo test # error here


      Is there any way to export script's functions to parent scope?

      Without writing to .bashrc, it's too global

      Same as .bashrc but for current bash instance only...







      bash shell function






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 11 mins ago









      ivan_pozdeev

      35219




      35219










      asked May 3 '14 at 15:56









      vp_arth

      1084




      1084




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You could source the file init.sh. No need to export the function in that file.



          $ cat init.bash 
          foo() echo $1;


          And use it:



          $ . ./init.bash && foo test
          test


          Sourcing a file would execute commands from it in the current shell context. As such, the functions would be available in the parent.



          export would set the attribute for a variable that would be applicable for the current shell and subshells. Not the parent shell. You need to define the variable in the current shell context.






          share|improve this answer




















          • Exactly, what I looking for. Thanks
            – vp_arth
            May 3 '14 at 16:07










          Your Answer








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

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

          else
          createEditor();

          );

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



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f127700%2finclude-a-bash-function-into-the-parent-script%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          You could source the file init.sh. No need to export the function in that file.



          $ cat init.bash 
          foo() echo $1;


          And use it:



          $ . ./init.bash && foo test
          test


          Sourcing a file would execute commands from it in the current shell context. As such, the functions would be available in the parent.



          export would set the attribute for a variable that would be applicable for the current shell and subshells. Not the parent shell. You need to define the variable in the current shell context.






          share|improve this answer




















          • Exactly, what I looking for. Thanks
            – vp_arth
            May 3 '14 at 16:07














          up vote
          3
          down vote



          accepted










          You could source the file init.sh. No need to export the function in that file.



          $ cat init.bash 
          foo() echo $1;


          And use it:



          $ . ./init.bash && foo test
          test


          Sourcing a file would execute commands from it in the current shell context. As such, the functions would be available in the parent.



          export would set the attribute for a variable that would be applicable for the current shell and subshells. Not the parent shell. You need to define the variable in the current shell context.






          share|improve this answer




















          • Exactly, what I looking for. Thanks
            – vp_arth
            May 3 '14 at 16:07












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          You could source the file init.sh. No need to export the function in that file.



          $ cat init.bash 
          foo() echo $1;


          And use it:



          $ . ./init.bash && foo test
          test


          Sourcing a file would execute commands from it in the current shell context. As such, the functions would be available in the parent.



          export would set the attribute for a variable that would be applicable for the current shell and subshells. Not the parent shell. You need to define the variable in the current shell context.






          share|improve this answer












          You could source the file init.sh. No need to export the function in that file.



          $ cat init.bash 
          foo() echo $1;


          And use it:



          $ . ./init.bash && foo test
          test


          Sourcing a file would execute commands from it in the current shell context. As such, the functions would be available in the parent.



          export would set the attribute for a variable that would be applicable for the current shell and subshells. Not the parent shell. You need to define the variable in the current shell context.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 3 '14 at 15:59









          devnull

          8,47112842




          8,47112842











          • Exactly, what I looking for. Thanks
            – vp_arth
            May 3 '14 at 16:07
















          • Exactly, what I looking for. Thanks
            – vp_arth
            May 3 '14 at 16:07















          Exactly, what I looking for. Thanks
          – vp_arth
          May 3 '14 at 16:07




          Exactly, what I looking for. Thanks
          – vp_arth
          May 3 '14 at 16:07

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f127700%2finclude-a-bash-function-into-the-parent-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?