-5

I know that bash supports some kind of list data structure:

list="1 2 3"
for i in $list; do
  echo "$i"
done

But what about arrays? Does bash have array data structure? And if so, how it would look like?

3

2 Answers 2

1

Bash supports one dimensional arrays.

Setting:

my_array=(1 2 3)
my_array[0]=0
my_array[2]=2

Retrieving:

echo ${my_array[1]}
2

echo ${my_array[@]}     # all values
0 2 2
Sign up to request clarification or add additional context in comments.

Comments

1

There's a few different notations for arrays in bash.

You can define an array like name[index]=value, or like name=(val1 val2 val3), or explicitly declare an array using declare -a ARRAYNAME. You can reference individual elements of an array with ${name[index]} or the whole array with ${name[@]}.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.