johny139
johny139

Reputation: 1

How to assign values to the correct array index?

Is it possible when I want to assign values to the correct index in array (array in bash)? when I enter some number I want that this number was on the same position as its value..

example: when I enter number 25 and then use command echo ${array[25]} I expect 25

I want to give numbers from shuf -i 1-49 -n 7 | xargs -n7 to array. Every number on his position.

Thanks for answers :)

Upvotes: 0

Views: 86

Answers (1)

user000001
user000001

Reputation: 33357

You could just loop over the result of shuf:

#!/bin/bash
while read i
do
  a[$i]=$i
done < <( shuf -i 1-49 -n 7 )

for i in "${!a[@]}"; do
  printf "%s\t%s\n" "$i" "${a[$i]}"
done

Upvotes: 1

Related Questions