#!/bin/bash
# ifs.sh

var1="a+b+c"
var2="d-e-f"
var3="g,h,i"

IFS=+
# The plus sign will be interpreted as a separator.
echo $var1     # a b c
echo $var2     # d-e-f
echo $var3     # g,h,i

echo

IFS="-"
# The plus sign reverts to default interpretation.
# The minus sign will be interpreted as a separator.
echo $var1     # a+b+c
echo $var2     # d e f
echo $var3     # g,h,i

echo

IFS=","
# The comma will be interpreted as a separator.
# The minus sign reverts to default interpretation.
echo $var1     # a+b+c
echo $var2     # d-e-f
echo $var3     # g h i

echo

IFS=" "
# The space character will be interpreted as a separator.
# The comma reverts to default interpretation.
echo $var1     # a+b+c
echo $var2     # d-e-f
echo $var3     # g,h,i

# ======================================================== #

# However ...
# $IFS traite les blancs différemment des autres caractères.

affiche_un_argument_par_ligne()
{
  for arg
  do 
    echo "[$arg]"
  done #  ^    ^   Entouré de crochets, juste pour le plaisir des yeux.
}

echo; echo "IFS=\" \""
echo "-------"

IFS=" "
var=" a  b c   "
#    ^ ^^   ^^^
affiche_un_argument_par_ligne $var  # affiche_un_argument_par_ligne `echo " a  b c   "`
# [a]
# [b]
# [c]


echo; echo "IFS=:"
echo "-----"

IFS=:
var=":a::b:c:::"        # Comme ci-dessus, mais en remplaçant " " par ":".
#    ^ ^^   ^^^         #+ mais en remplaçant " " par ":"
affiche_un_argument_par_ligne $var
# []
# [a]
# []
# [b]
# [c]
# []
# []
# []

# Remarquez les crochets "vides".
# La même chose se produit avec le séparateur de champs "FS" dans awk.

echo

exit 0
