0

I am learning shell script language. I write and run the code using Ubuntu terminal.I have declare an array with value like below..

arr=(1 2 3)
echo $(arr[0])
echo $(arr[1])
echo $(arr[2])

The above code doesn't run.Whenever I run using the terminal,I face error like below..

./a.sh: line 2: arr[0]: command not found
./a.sh: line 3: arr[1]: command not found
./a.sh: line 4: arr[2]: command not found

How can I solve it?

1
  • Note: arrays won't work in (da)sh, however the error format suggests you did use bash here. Jun 24, 2022 at 7:09

1 Answer 1

2

The syntax $(...) is for executing sub-shell commands.

Use syntax ${...} to read variable content. So:

arr=(1 2 3)
echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.