Fill shell script array in for loop

I have a list of data that the user gives as arguments.

For simplicity-sake of the code that comes after the one shown, this code builds a relationship between the value inputted and the position it was inputted.

values=("${@:2}")
values2=()
for i in ${!values[@]}; do values2[${values[$i]}]="$i" echo "$values2"
done
echo $values2

This code worked in ubuntu 16.04 but doesn't work on ubuntu 18.04. I don't know what I'm missing.

In this code, no errors appear and, instead, at the end of this code snippet, the variable values2 is always empty.
If I call this code using thisscript param1 1600 2000 3000, after that for, which is ran, $values2 stays empty.

What is deleting $values2?

2 Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like