Search code examples
bashifs

Setting IFS to ignore whitespaces while reading from a here-string


I have a little problem with IFS to distinguish packages and the distribution. Now I wanted to add the version number of CentOS, but the problem is that CentOS and the number 6 are separated by IFS. My question is, how do I get IFS to ignore spaces and see them as a single value? Thanks in advance :)

...

PACKET_MANAGERS=(
    "Debian|package:package:package|apt:dpkg-query|"
    "CentOS 6|package:package:package|yum:rpm|${CentOS_6_REPO}"
    ...
)
...
   for system in ${PACKET_MANAGERS[@]}
do
    IFS='|' read -r -a data <<< $system
    debug "Testing ${system} => ${data[0]}"

    if echo "${SYSTEM_NAME}" | grep "${data[0]}" &>/dev/null; then
        SYSTEM_NAME_DETECTED="${data[0]}"
    else
        continue
    fi

...

done

Solution

  • You need to quote ${PACKET_MANAGERS[@]}:

    for system in "${PACKET_MANAGERS[@]}"
    

    Otherwise, word-splitting will be done on the result of expanding the array expression, so each word in each element of the array will be a separate iteration of the for loop. Using [@] will make each array element be a separate word in the result.