Vrushab Maitri
Vrushab Maitri

Reputation: 350

Error using arrays in shell script

I was testing a shell script in which arrays were used. This is an example taken from tutorialspoint

#!/bin/sh

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"

But I am getting this error
test.sh: 3: test.sh: NAME[0]=Zara: not found
test.sh: 4: test.sh: NAME[1]=Qadir: not found
test.sh: 5: test.sh: NAME[2]=Mahnaz: not found
test.sh: 6: test.sh: NAME[3]=Ayan: not found
test.sh: 7: test.sh: NAME[4]=Daisy: not found
test.sh: 8: test.sh: Bad substitution

The link to the exact page is here

Upvotes: 0

Views: 394

Answers (2)

pro_cheats
pro_cheats

Reputation: 1582

Simple. You just need to run as -

bash script_name.sh

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

The shebang is wrong, this only works in specific shells, e.g. bash.

Upvotes: 4

Related Questions