You should take care when resurrecting old threads. It should generally only be done when you have something significant to add to them. And make sure you clearly point out that you are doing so, otherwise people tend to overlook the dates and start replying to a long-dead conversation.
That said, you did post something significant.
But I'd still say arrays are the way to go. Resetting the IFS works, but to my mind is best used to convert strings into arrays for further use.
(Arrays are painfully underutilized in bash scripting, IMO. Any time you have a list of strings to work on, filenames, options, etc., put it into an array first.)
And now to add my own observation...this part of gloriant's post above can be made much cleaner:
#set first empty position with new value
This is an awkward way to add elements to an array. You can use this instead:
options+=( "option 1" "option 2" "quit" )
tells it to append the elements to an existing array, while array=()
will reset it with the new values. Either one could be used here, since you're starting with an empty array anyway.
Combine this with IFS to split a string stored in a variable:
optionstring="option 1,option 2,quit"
options=( $optionstring )
Notice that this is one of the rare instances where you do not want to quote the variable. The last line also demonstrates how you can manually reset IFS to the default value ($'' is ansi-c style quoting. See the QUOTING section of the bash man page).
It's also possible to simply unset IFS
, and the shell will behave as if it were set to the default.