0

we want to set variable that includes words as array

folder_mount_point_list="sdb sdc sdd sde sdf sdg"
ARRAY=( $folder_mount_point_list )

but when we want to print the first array value we get all words

echo ${ARRAY[0]}
sdb sdc sdd sde sdf sdg

expected results

echo ${ARRAY[0]}
sdb

echo ${ARRAY[1]}
sdc

how to convert variable to array?

  • 3
    Can not reproduce. What's the value of $IFS? – Isaac Dec 31 '17 at 14:31
  • we not set this value in my bash script – yael Dec 31 '17 at 14:34
  • And if you do add IFS=$' \t\n' to your script? – Hauke Laging Dec 31 '17 at 14:38
  • ok now its works , can I do unset IFS after ARRAY=( $folder_mount_point_list ) – yael Dec 31 '17 at 14:40
  • Why would you want to unset IFS instead of keeping it at its? Unsetting IFS has the same effect as setting it like IFS=$' \t\n' – Hauke Laging Dec 31 '17 at 14:46
3

It seems that you have (maybe unintentionally) changed the important shell variable IFS in the script. Restoring it to its usual value or unsetting it (i.e. activating its default value) solves the problem:

IFS=$' \t\n'

or

unset IFS
  • On my system unsetting IFS doesn't restore its default value. I cannot find such statement in bash manual either. Where did you get if from? – jimmij Dec 31 '17 at 15:06
  • @jimmij Not "restore" in the sense that $IFS contains non-empty data after being unset but after unsetting IFS bash behaves like IFS having its default value. – Hauke Laging Dec 31 '17 at 15:31
  • @jimmij Search in bash manual for If IFS is unset, or its value is exactly <space><tab><newline>,. An unset IFS is functionally equivalent to the default, not equal though, as var=IFS sets var to null, not <space><tab><newline>. – Isaac Dec 31 '17 at 16:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.