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
bash function syntax
add a comment |
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
bash function syntax
add a comment |
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
bash function syntax
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
bash function syntax
edited Nov 14 at 23:34
codeforester
17.3k83864
17.3k83864
asked Nov 14 at 22:55
Giuseppe Minardi
508
508
add a comment |
add a comment |
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; ... }
add a comment |
up vote
2
down vote
You need a space after the name of your function and before the {:
function doi {
^
2
Right. (Well, strictly speaking, you don't. Bash lets you define a function nameddoi{-- but then you'd need a{token after the function name.) The issue is thatdoi{is a single token, not an identifierdoifollowed by a{token.
– Keith Thompson
Nov 14 at 23:14
add a comment |
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; ... }
add a comment |
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; ... }
add a comment |
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; ... }
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; ... }
edited Nov 14 at 23:37
answered Nov 14 at 23:18
codeforester
17.3k83864
17.3k83864
add a comment |
add a comment |
up vote
2
down vote
You need a space after the name of your function and before the {:
function doi {
^
2
Right. (Well, strictly speaking, you don't. Bash lets you define a function nameddoi{-- but then you'd need a{token after the function name.) The issue is thatdoi{is a single token, not an identifierdoifollowed by a{token.
– Keith Thompson
Nov 14 at 23:14
add a comment |
up vote
2
down vote
You need a space after the name of your function and before the {:
function doi {
^
2
Right. (Well, strictly speaking, you don't. Bash lets you define a function nameddoi{-- but then you'd need a{token after the function name.) The issue is thatdoi{is a single token, not an identifierdoifollowed by a{token.
– Keith Thompson
Nov 14 at 23:14
add a comment |
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 {
^
You need a space after the name of your function and before the {:
function doi {
^
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 nameddoi{-- but then you'd need a{token after the function name.) The issue is thatdoi{is a single token, not an identifierdoifollowed by a{token.
– Keith Thompson
Nov 14 at 23:14
add a comment |
2
Right. (Well, strictly speaking, you don't. Bash lets you define a function nameddoi{-- but then you'd need a{token after the function name.) The issue is thatdoi{is a single token, not an identifierdoifollowed 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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