user2185454
user2185454

Reputation: 27

Badly placed ()'s error with the following shell script

This is the code snippet. Here i am seeing the badly placed ()'s error

#!/bin/sh 
#!/usr/bin/perl -w

# array declaration
arr= (one two three)  # seeing error here

# for loop
for (( i=0;i<4;i++ ))
do
    echo "\n $i : ${a[i]}"
done

Upvotes: 2

Views: 42879

Answers (2)

Antarus
Antarus

Reputation: 1621

It is a small error.

arr= (one two three)

should've been

arr=(one two three)

Also you can't use \n in echo. Use printf if you want to use \n.

And fixing the rest of the errors, the code looks like this.

# array declaration
arr=(one two three)  

# for loop
for (( i=0;i<3;i++ ))
do
    printf "\n $((i+1)) : ${arr[i]}"
done
echo ""

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

arr= (one two three)

Lets break down what this does.

arr=

This part assigns $arr an empty value (temporarily, since it precedes a command).

(one two three)

This part runs one in a subshell with arguments two and three, with the previously assigned value of $arr.

Did you perhaps mean to assign the three values to an array in $arr instead?

Upvotes: 1

Related Questions