LDP/LDP/guide/docbook/abs-guide/ifs.sh

48 lines
616 B
Bash

#!/bin/bash
# ifs.sh
# $IFS treats whitespace differently than other characters.
output_args_one_per_line()
{
for arg
do echo "[$arg]"
done
}
echo; echo "IFS=\" \""
echo "-------"
IFS=" "
var=" a b c "
output_args_one_per_line $var # output_args_one_per_line `echo " a b c "`
#
# [a]
# [b]
# [c]
echo; echo "IFS=:"
echo "-----"
IFS=:
var=":a::b:c:::" # Same as above, but substitute ":" for " ".
output_args_one_per_line $var
#
# []
# [a]
# []
# [b]
# [c]
# []
# []
# []
# The same thing happens with the "FS" field separator in awk.
# Thank you, Stephane Chazelas.
echo
exit 0