Why is my array incorrect for my shell script?

I searched everywhere for the answer to this question. I have an array in my shell script, but when I run it, I get this error: "(" unexpected

What am I doing wrong here:

array=( 1 2 3 4 5 )

I am using Ubuntu 11.10

+4
source share
4 answers

You are using a script with /bin/sh , not /bin/bash . There are no arrays in sh .

 choroba@cyan ~$ /bin/sh $ a=( 1 2 3 ) /bin/sh: Syntax error: "(" unexpected 
+10
source

do you use bash?

 $ bash --version GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11) Copyright (C) 2007 Free Software Foundation, Inc. $ array=( 1 2 3 4 5 ) $ echo ${array[1]} 2 $ 
0
source

Also sometime this is due to extra space:

 array = (1 2 3 4) 

wrong. It should be:

 array=(1 2 3 4) 
0
source

Try specifying the word bash when running the script as follows:

 $ bash script.sh 
0
source

Source: https://habr.com/ru/post/1385482/


All Articles