1

My version of bash is:

bash --version GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu) 

If I do the prototype function

#!/bin/bash function f() { echo "hello" $1 } f "world" 

I get Syntax error: "(" unexpected

Why is that?

Output of shopt is:

autocd off cdable_vars off cdspell off checkhash off checkjobs off checkwinsize on cmdhist on compat31 off compat32 off compat40 off compat41 off direxpand off dirspell off dotglob off execfail off expand_aliases on extdebug off extglob on extquote on failglob off force_fignore on globstar off gnu_errfmt off histappend on histreedit off histverify off hostcomplete off huponexit off interactive_comments on lastpipe off lithist off login_shell off mailwarn off no_empty_cmd_completion off nocaseglob off nocasematch off nullglob off progcomp on promptvars on restricted_shell off shift_verbose off sourcepath on xpg_echo off 
6
  • works for me.. make sure that there are no special, hidden, characters in the file Commented Jan 19, 2014 at 0:50
  • How strange. Any tips on how to look for these characters? Commented Jan 19, 2014 at 0:54
  • Try cat -A filename. It expands control characters to printable forms, and adds a $ to the end of each line. Commented Jan 19, 2014 at 0:55
  • 1
    How are you running the file? The error message sounds like it is coming from ksh or some other non-bash shell. Commented Jan 19, 2014 at 0:57
  • 2
    I get that error if I run the script with dash foo.bash or sh foo.bash (/bin/sh is a symlink to dash on my system). The #!/bin/bash line causes your script to be interpreted by bash -- but only if you invoke it directly, not if you feed it to some other shell. Commented Jan 19, 2014 at 1:00

1 Answer 1

6

Your version of bash does accept the function keyword. The problem is that you're not running your script under bash.

I get the same error if I run the script with dash foo.bash or sh foo.bash (/bin/sh is a symlink to dash on my system).

dash doesn't recognize the function syntax.

The #!/bin/bash line causes your script to be interpreted by bash -- but only if you invoke it directly, not if you feed it to some other shell.

Rather than invoking a shell and passing your script name to it as an argument:

sh foo.bash 

just invoke your script directly:

foo.bash (or ./foo.bash) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.