0

I have created a shell script named "script.sh" which reads an arrays elements and prints it on terminal. The script is as follows:

 arr=("hello" "world")
for i in ${arr[@]}
do
echo  $i;
done

It gives expected output i.e 'Hello World' on executing it in terminal, but if I schedule the same script in crontab jobs to get executed automatically every minute and store the output in another file,the job fails and gives

/home/vikash/script.sh: 1: Syntax error: "(" unexpected error.

The crontab job to execute the script every minute and store the output in another file is as follows:

* * * * * $HOME/script.sh >> $HOME/output.log 2>&1

How to use array in this scenario?? please help.

vikash
  • 53
  • 3
  • 11

2 Answers2

5

Add this before the first line of your script:

#!/bin/bash
  • in my case the shebang was not on the firstline (I had a # line before the shebang). So yes it must be absolutely the first line of the script. – Nico Jun 13 '23 at 15:20
0

if this question is still valid, just run your script with

bash script.sh

I also tried it with sh somehow it is not working with sh but with bash it suns smooth.

Umesh Kaushik
  • 1,551
  • 1
  • 16
  • 18