Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I accidentally input the the following command into the Bash terminal:

exit()

And the Bash keeps read the next line until I input a string literal and Bash gives the error of "-bash: syntax error near unexpected token".

So I test some other similar inputs like:

foobar ()

The Bash gives the similar output. The following is the result I got:

~ $ foo ()
> bar
-bash: syntax error near unexpected token `bar'

Can someone explain why. I only know the parentheses are used to create arrays or subshell. I don't know what this one actually does.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

That's the syntax for defining a shell function. See the Shell Functions section of the Bash manual. It begins:

Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular" command. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed. Shell functions are executed in the current shell context; no new process is created to interpret them.

Functions are declared using this syntax:

name () compound-command [ redirections ]

It's expecting you to type a compound command. For example:

$ foo()
> {
> echo 'you said foo'
> }
$ foo
you said foo
share|improve this answer
    
Thanks! I just forgot about functions. It seems so weird to write only a single function header without blocks in the command line instead of in the script. I was totally confused at first. –  moleculea Nov 24 '13 at 6:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.