0

A simple shell script used for creating some system reporting contains the code

ARRAY=$(awk -F ':' '$3>=1000 && $3<60000 {print $1}' /etc/passwd)
ARRAY+=('root') 

The array's declaration in the first line doesn't produce any error output, but for the second line I get an error message saying: Syntax error: word unexpected (expecting ")")

What is wrong with this code?

5
  • What is your bash version? The first line does not mean you are working with an array, $(..) is a command substitution syntax. Either way it should not produce the error you said Commented Jun 18, 2017 at 10:27
  • 2
    Use: ARRAY=($(awk -F ':' '$3>=1000 && $3<60000 {print $1}' /etc/passwd)) Commented Jun 18, 2017 at 10:31
  • Éxactly, it should work like Cyrus wrote. However, I then get this error for the initial array initialization: Syntax error: "(" unexpected - And if I leave out the outer brackets (s. above), the array gets initialized, but then the second line produces this totally obscure error. My bash version is 4.3.30. Commented Jun 18, 2017 at 10:38
  • I demonstrated this odd behavior and took a screenshot of it, which you can view here: Weird bash behavior Commented Jun 18, 2017 at 10:46
  • 2
    Important: sh is not bash Commented Jun 18, 2017 at 10:57

1 Answer 1

3

The problem, from your screenshot, is that you are not running it as a bash script. You're doing sh testscript which executes it with the sh shell, which is not bash. Make the changes that @Cyrus recommended and either make the script executable so you use the shebang line and do ./testscript or use bash to run it bash testscript

Also, set +x turns off tracing/debugging so when you're hitting unexpected problems you'd be better using set -x to turn it on for the block in question.

I demonstrated the problem for myself as follows:

$ cat testscript
ARRAY=$(awk -F ':' '$3>=1000 && $3<60000 {print $1}' /etc/passwd)
ARRAY+=('root')
$ sh testscript
testscript: 2: testscript: Syntax error: word unexpected (expecting ")"
$ bash testscript
$
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for your answer, it now indeed behaves in the way I would have expected it to behave. Still I really wonder why sh (after having initialized the array successfully with the right values when leaving out the outer brackets) produces this (to me) totally weird syntax error for ARRAY+=('root') - Why does it expect a closing bracket in this line, although there already is an opening and a closing bracket?
@c128linux It seems to be treating the line as though it's doing name() to define a function, in this case it would be named ARRAY+=. There's an unexpected word in the middle of the parens though. If you take 'root' out it will then complain about an illegal function name
Thanks for this in-depth explanation! @Cyrus Answer accepted ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.