0
declare -a identifications=(
'10760260000119 - 34234324' # ok
)

declare -a years=(
  2010
  2011
  2012
  2013
  2014
)

for identification in "${identifications[@]}"
do
  for year in "${years[@]}"
  do
    downloader --type=CNPJ --format=XLS --identification=${identification:0:14} --password=${identification:16:20} --competence=${year} --output="$identification - $year"
  done
done

I created this bash script using a MacBook Pro Yosemite 10 and now i'm trying to use in a ubuntu 14 server (AWS) and is causing a syntax error:

bash/2010.sh: 1: bash/2010.sh: Syntax error: "(" unexpected

Maybe i need install some package?

Thanks.

4
  • 1
    Do you have a #!/bin/bash at the top of your file? If not, add one. Commented Jan 19, 2016 at 15:50
  • I added.. same error. I'm using sh bash_program.sh Commented Jan 19, 2016 at 15:55
  • Either make it executable, or run it like bash bash_program.sh. Commented Jan 19, 2016 at 15:57
  • Now is working. Post as an answer Tom!! Commented Jan 19, 2016 at 15:58

1 Answer 1

1

Depending on your system, the program that is run when you use sh can vary. You're basically saying "use the default shell, whatever that is", so you should only use this if you're limiting yourself to POSIX-standard shell.

If sh means bash on your system, then it will be run in a compatibility mode, which can result in certain features (such as arrays) being disabled (I say "can" as I'm not sure of the details - this varies between platforms as you've seen).

Long story short, if you want to use bash features, you should run it like bash bash_program.sh instead.

Also, I think it's good practice to add the shebang line to the top of the file #!/bin/bash, make it executable and run it like ./bash_program.sh. This clearly states the intent at the top of the script.

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

2 Comments

On recent Ubuntus, sh is not bash in compatibility mode, but dash. It doesn't support arrays at all.
@choroba thanks, I've adjusted my answer to factor that in.

Your Answer

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