-1

According to several threads in SO, I've developed the following mini-script:

list=(Alabama Alaska "New York")

list=$list" Oregon"

for state in  "${list[@]}"
do
    echo The next state is $state
done

The problem is that the output is

The next state is Alabama Oregon
The next state is Alaska
The next state is New York

instead of

The next state is Alabama 
The next state is Alaska
The next state is New York
The next state is Oregon

How would I append the element Oregon?

An old man in the sea.
  • 1,169
  • 1
  • 13
  • 30
  • 2
    https://stackoverflow.com/questions/55316852/append-elements-to-an-array-in-bash https://stackoverflow.com/questions/39935353/bash-incrementally-adding-to-an-array https://stackoverflow.com/questions/52678358/how-to-append-values-to-an-array-in-bash-by-adding-one-element-to-the-previous-e https://stackoverflow.com/questions/1951506/add-a-new-element-to-an-array-without-specifying-the-index-in-bash – KamilCuk Jan 14 '23 at 11:58
  • If you refer to an array variable as if it were a plain (non-array) variable, bash (mostly) treats that as referring to the first element of the array. So `list=$list" Oregon"` appends to the first element of the array, rather than adding a new element. – Gordon Davisson Jan 14 '23 at 18:13

1 Answers1

0
list+=("Oregon")

See this answer: https://stackoverflow.com/a/39935409/2840436

micromoses
  • 6,747
  • 2
  • 20
  • 29