I was hoping a kind person more intelligent than me could help me out here.
I am working on a Bash script, and in it there is a for loop that will go around an unknown/undefined number of times.
Now, in this for loop, there will be a value assigned to a variable. Let's call this variabe: $var1
Each time the loop goes (and I will never know how many times it goes), I would like to assign the value inside $var1 to an array, slowly building up the array as it goes. Let's call the array $arr
This is what I have so far:
for i in $( seq 0 $unknown ); do
...
some commands that will make $var1 change...
...
arr=("${arr[@]}" "$var1")
done
However, when I want to echo or use the values in the array $arr, I get no results
Perhaps someone will kindly help me in the right direction?
I would really appreciate it so much.
arr+=("$var1")will populate the array inside the loop, and if you really want to define an empty array before/outside the loop,arr=()Ordeclare -a arrfor i in $(seq 0 $unknown)would be better written asfor ((i=0; i<unknown; i++)).seqis not a standardized command or part of the shell itself, and running an external command when not needed is generally inefficient. (The POSIX-compliant alternative is wordier:i=0; while [ "$i" -lt 10 ]; do ...; i=$((i + 1)); done-- there's a reason ksh and bash provide the shorter form).echo ${arr}only ever gave the first element.