2

Maybe I'm being really silly here but I can't seem to figure this out:

#!/usr/bin/env bash
...
DELTAS=($(ls -p /foo/bar/ | grep -P '^\d+[^\.]+\.sh$'))
...

If I run this interactively it's fine, and echo $DELTAS returns:

1-foo.sh
2-bar.sh

However, when this is run inside a bash script, I receive Syntax error: "(" unexpected; any ideas?

3
  • 1
    Please update your question to show us the entire script, including the #! line. Copy and paste it, don't re-type it. The error message implies that your script is being run using sh rather than bash. In a comment you say the script starts with #!/usr/bin/env bash (#!/bin/bash would be simpler). Are you certain you have the syntax of the #! line right? Commented Dec 13, 2015 at 21:53
  • 1
    Please take a look: shellcheck.net Commented Dec 13, 2015 at 22:29
  • Execute that script file as bash <file name>. Commented Dec 14, 2015 at 3:27

1 Answer 1

1

I'm guessing that you've not put #!/bin/bash at the top of your script, and so it's running using /bin/sh rather than /bin/bash. This would run under dash rather than bash on Ubuntu, for example.

That syntax is a bash extension.

Sign up to request clarification or add additional context in comments.

4 Comments

My scripts start with #!/usr/bin/env bash, but still nope :(
If you execute it with sh myscript.sh, though, the shebang is ignored.
Try bash ./scriptname.sh ?
Winner! I knew it was something silly!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.