Skip to main content
added 88 characters in body
Source Link
JoL
  • 5k
  • 1
  • 20
  • 37

Despite the other answers explaining how to fix the script, I wondered what the error was technically about. I think the only valid syntax where bash allows a word in a command to be an unquoted and unescaped ( is when defining functions. So, bash saw ( and immediately thought it had to be a definition for a function named [, but then you followed with '$#' and bash was only expecting ), so that's why you got that syntax error. If it wasn't meant to be a function definition, then who knows what it was meant to be. Anything else after ( besides ) would have raised the same syntax error:

$ echo ( foo )
bash: syntax error near unexpected token `foo'

If you remove what follows ( that doesn't fit the syntax of a function definition, you get:

$ [ ( ) ( -f "$1" )

Which ends up defining the function [:

$ [
bash: -f: command not found

Despite the other answers explaining how to fix the script, I wondered what the error was technically about. I think the only valid syntax where bash allows a word in a command to be an unquoted and unescaped ( is when defining functions. So, bash saw ( and immediately thought it had to be a definition for a function named [, but then you followed with '$#' and bash was only expecting ), so that's why you got that syntax error. Anything else after ( besides ) would have raised the same syntax error:

$ echo ( foo )
bash: syntax error near unexpected token `foo'

If you remove what follows ( that doesn't fit the syntax of a function definition, you get:

$ [ ( ) ( -f "$1" )

Which ends up defining the function [:

$ [
bash: -f: command not found

Despite the other answers explaining how to fix the script, I wondered what the error was technically about. I think the only valid syntax where bash allows a word in a command to be an unquoted and unescaped ( is when defining functions. So, bash saw ( and immediately thought it had to be a definition for a function named [, but then you followed with '$#' and bash was only expecting ), so that's why you got that syntax error. If it wasn't meant to be a function definition, then who knows what it was meant to be. Anything else after ( besides ) would have raised the same syntax error:

$ echo ( foo )
bash: syntax error near unexpected token `foo'

If you remove what follows ( that doesn't fit the syntax of a function definition, you get:

$ [ ( ) ( -f "$1" )

Which ends up defining the function [:

$ [
bash: -f: command not found
Source Link
JoL
  • 5k
  • 1
  • 20
  • 37

Despite the other answers explaining how to fix the script, I wondered what the error was technically about. I think the only valid syntax where bash allows a word in a command to be an unquoted and unescaped ( is when defining functions. So, bash saw ( and immediately thought it had to be a definition for a function named [, but then you followed with '$#' and bash was only expecting ), so that's why you got that syntax error. Anything else after ( besides ) would have raised the same syntax error:

$ echo ( foo )
bash: syntax error near unexpected token `foo'

If you remove what follows ( that doesn't fit the syntax of a function definition, you get:

$ [ ( ) ( -f "$1" )

Which ends up defining the function [:

$ [
bash: -f: command not found