0

we set the "list" variable with sdb - sdz range

# MAX=z
# list=$(eval echo sd{b..$MAX})
# echo $list
sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz

until now its ok

now we set the $list in array as the follwing

#array=( echo $list )

but when we print the first value of the array we get "echo"

#echo ${array[0]}
echo

what is wrong here?

expected results:

#echo ${array[0]}
sdb


#echo ${array[1]}
sdc

#echo ${array[2]}
sdc

or

# echo  "${list[counter++]}"
2
  • array=( echo $list ) populates array with echo then the contents of $list after word splitting, file name, expansion, etc. Google how to use arrays, shell quoting rules, $( foo ) vs ( foo ), etc. Commented Jan 15, 2018 at 20:07
  • Either do array=($list), array=( $(echo $list)) but the latter is not best practice. (Neither is, really!) Just assign the array directly. Commented Jan 15, 2018 at 20:09

1 Answer 1

1
$ max=z
$ list=( $(eval echo sd{b..$max}) )
$ echo "${list[*]}"
sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo sdp sdq sdr sds sdt sdu sdv sdw sdx sdy sdz
$ echo "${list[1]}"
sdc
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.