1

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.

1 Answer 1

4

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
1
  • 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, 2013 at 6:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .