Why are spaces required before a curly brace in a function definition?











up vote
2
down vote

favorite












I'm trying to create a bash script that converts a bunch of pdfs into text in order to extract some information but the shell gives me this error:



./AutoBib.sh: line 8: syntax error near unexpected token `pdftotext'
./AutoBib.sh: line 8: ` pdftotext $1 temp.txt'


Here there is an example of my function:



function doi{

pdftotext $1 temp.txt
cat temp.txt | grep doi: | cut -d: -f 2 | head -n 1 >> dois.txt
rm -rf temp.txt
}
doi $PDF


Where the variable PDF is taken in input. Before adding the function it worked, I used to write in my script:



pdftotext $PDF tempo.txt









share|improve this question




























    up vote
    2
    down vote

    favorite












    I'm trying to create a bash script that converts a bunch of pdfs into text in order to extract some information but the shell gives me this error:



    ./AutoBib.sh: line 8: syntax error near unexpected token `pdftotext'
    ./AutoBib.sh: line 8: ` pdftotext $1 temp.txt'


    Here there is an example of my function:



    function doi{

    pdftotext $1 temp.txt
    cat temp.txt | grep doi: | cut -d: -f 2 | head -n 1 >> dois.txt
    rm -rf temp.txt
    }
    doi $PDF


    Where the variable PDF is taken in input. Before adding the function it worked, I used to write in my script:



    pdftotext $PDF tempo.txt









    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm trying to create a bash script that converts a bunch of pdfs into text in order to extract some information but the shell gives me this error:



      ./AutoBib.sh: line 8: syntax error near unexpected token `pdftotext'
      ./AutoBib.sh: line 8: ` pdftotext $1 temp.txt'


      Here there is an example of my function:



      function doi{

      pdftotext $1 temp.txt
      cat temp.txt | grep doi: | cut -d: -f 2 | head -n 1 >> dois.txt
      rm -rf temp.txt
      }
      doi $PDF


      Where the variable PDF is taken in input. Before adding the function it worked, I used to write in my script:



      pdftotext $PDF tempo.txt









      share|improve this question















      I'm trying to create a bash script that converts a bunch of pdfs into text in order to extract some information but the shell gives me this error:



      ./AutoBib.sh: line 8: syntax error near unexpected token `pdftotext'
      ./AutoBib.sh: line 8: ` pdftotext $1 temp.txt'


      Here there is an example of my function:



      function doi{

      pdftotext $1 temp.txt
      cat temp.txt | grep doi: | cut -d: -f 2 | head -n 1 >> dois.txt
      rm -rf temp.txt
      }
      doi $PDF


      Where the variable PDF is taken in input. Before adding the function it worked, I used to write in my script:



      pdftotext $PDF tempo.txt






      bash function syntax






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 at 23:34









      codeforester

      17.3k83864




      17.3k83864










      asked Nov 14 at 22:55









      Giuseppe Minardi

      508




      508
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          From Bash manual:




          The braces are reserved words, so they must be separated from the list
          by blanks or other shell metacharacters.




          function ... is an outdated syntax for defining Bash functions. Use this instead:



          doi() {
          ...
          }


          Since () are meta characters, you don't need a space in this case (though spaces make your code prettier):



          doi(){
          ...
          }


          Extending this a little, remember that we need a whitespace (space, tab, or newline) after { and before `}' in command grouping, like this:



          { command1; command2; ... }





          share|improve this answer






























            up vote
            2
            down vote













            You need a space after the name of your function and before the {:



            function doi {
            ^





            share|improve this answer

















            • 2




              Right. (Well, strictly speaking, you don't. Bash lets you define a function named doi{ -- but then you'd need a { token after the function name.) The issue is that doi{ is a single token, not an identifier doi followed by a { token.
              – Keith Thompson
              Nov 14 at 23:14











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f53309949%2fwhy-are-spaces-required-before-a-curly-brace-in-a-function-definition%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








            up vote
            5
            down vote



            accepted










            From Bash manual:




            The braces are reserved words, so they must be separated from the list
            by blanks or other shell metacharacters.




            function ... is an outdated syntax for defining Bash functions. Use this instead:



            doi() {
            ...
            }


            Since () are meta characters, you don't need a space in this case (though spaces make your code prettier):



            doi(){
            ...
            }


            Extending this a little, remember that we need a whitespace (space, tab, or newline) after { and before `}' in command grouping, like this:



            { command1; command2; ... }





            share|improve this answer



























              up vote
              5
              down vote



              accepted










              From Bash manual:




              The braces are reserved words, so they must be separated from the list
              by blanks or other shell metacharacters.




              function ... is an outdated syntax for defining Bash functions. Use this instead:



              doi() {
              ...
              }


              Since () are meta characters, you don't need a space in this case (though spaces make your code prettier):



              doi(){
              ...
              }


              Extending this a little, remember that we need a whitespace (space, tab, or newline) after { and before `}' in command grouping, like this:



              { command1; command2; ... }





              share|improve this answer

























                up vote
                5
                down vote



                accepted







                up vote
                5
                down vote



                accepted






                From Bash manual:




                The braces are reserved words, so they must be separated from the list
                by blanks or other shell metacharacters.




                function ... is an outdated syntax for defining Bash functions. Use this instead:



                doi() {
                ...
                }


                Since () are meta characters, you don't need a space in this case (though spaces make your code prettier):



                doi(){
                ...
                }


                Extending this a little, remember that we need a whitespace (space, tab, or newline) after { and before `}' in command grouping, like this:



                { command1; command2; ... }





                share|improve this answer














                From Bash manual:




                The braces are reserved words, so they must be separated from the list
                by blanks or other shell metacharacters.




                function ... is an outdated syntax for defining Bash functions. Use this instead:



                doi() {
                ...
                }


                Since () are meta characters, you don't need a space in this case (though spaces make your code prettier):



                doi(){
                ...
                }


                Extending this a little, remember that we need a whitespace (space, tab, or newline) after { and before `}' in command grouping, like this:



                { command1; command2; ... }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 14 at 23:37

























                answered Nov 14 at 23:18









                codeforester

                17.3k83864




                17.3k83864
























                    up vote
                    2
                    down vote













                    You need a space after the name of your function and before the {:



                    function doi {
                    ^





                    share|improve this answer

















                    • 2




                      Right. (Well, strictly speaking, you don't. Bash lets you define a function named doi{ -- but then you'd need a { token after the function name.) The issue is that doi{ is a single token, not an identifier doi followed by a { token.
                      – Keith Thompson
                      Nov 14 at 23:14















                    up vote
                    2
                    down vote













                    You need a space after the name of your function and before the {:



                    function doi {
                    ^





                    share|improve this answer

















                    • 2




                      Right. (Well, strictly speaking, you don't. Bash lets you define a function named doi{ -- but then you'd need a { token after the function name.) The issue is that doi{ is a single token, not an identifier doi followed by a { token.
                      – Keith Thompson
                      Nov 14 at 23:14













                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    You need a space after the name of your function and before the {:



                    function doi {
                    ^





                    share|improve this answer












                    You need a space after the name of your function and before the {:



                    function doi {
                    ^






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 at 22:58









                    damienfrancois

                    24.9k54261




                    24.9k54261








                    • 2




                      Right. (Well, strictly speaking, you don't. Bash lets you define a function named doi{ -- but then you'd need a { token after the function name.) The issue is that doi{ is a single token, not an identifier doi followed by a { token.
                      – Keith Thompson
                      Nov 14 at 23:14














                    • 2




                      Right. (Well, strictly speaking, you don't. Bash lets you define a function named doi{ -- but then you'd need a { token after the function name.) The issue is that doi{ is a single token, not an identifier doi followed by a { token.
                      – Keith Thompson
                      Nov 14 at 23:14








                    2




                    2




                    Right. (Well, strictly speaking, you don't. Bash lets you define a function named doi{ -- but then you'd need a { token after the function name.) The issue is that doi{ is a single token, not an identifier doi followed by a { token.
                    – Keith Thompson
                    Nov 14 at 23:14




                    Right. (Well, strictly speaking, you don't. Bash lets you define a function named doi{ -- but then you'd need a { token after the function name.) The issue is that doi{ is a single token, not an identifier doi followed by a { token.
                    – Keith Thompson
                    Nov 14 at 23:14


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2fstackoverflow.com%2fquestions%2f53309949%2fwhy-are-spaces-required-before-a-curly-brace-in-a-function-definition%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

                    How to send String Array data to Server using php in android

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Is anime1.com a legal site for watching anime?