1

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?

5
  • 3
    Can not reproduce. What's the value of $IFS?
    – user232326
    Commented Dec 31, 2017 at 14:31
  • we not set this value in my bash script
    – yael
    Commented Dec 31, 2017 at 14:34
  • And if you do add IFS=$' \t\n' to your script? Commented Dec 31, 2017 at 14:38
  • ok now its works , can I do unset IFS after ARRAY=( $folder_mount_point_list )
    – yael
    Commented Dec 31, 2017 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' Commented Dec 31, 2017 at 14:46

1 Answer 1

4

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
3
  • 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
    Commented Dec 31, 2017 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. Commented Dec 31, 2017 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>.
    – user232326
    Commented Dec 31, 2017 at 16:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.