I want to write a bash script that will get user input and store it in an array. Input: 1 4 6 9 11 17 22
I want this to be saved as array.
I want to write a bash script that will get user input and store it in an array. Input: 1 4 6 9 11 17 22
I want this to be saved as array.
read it like this:
read -a arr
read -a arr <<< "1 4 6 9 11 17 22"
print # of elements in array:
echo ${#arr[@]}
OR loop through the above array
for i in ${arr[@]}
do
echo $i # or do whatever with individual element of the array
done
for i in "${arr[@]}"How about this:
while read line
do
my_array=("${my_array[@]}" $line)
done
printf -- 'data%s ' "${my_array[@]}"
Hit Ctrl-D to stop entering numbers.
my_array+=( $line ) would be much simpler.