Sometimes it can be extremely convenient to have them and having a simple example would help. Also it might help to show some broken behavior of IFS=\n, which can be very surprising.
# https://stackoverflow.com/a/53747300
s='A|B|C|D' # specify your "array" as a string with a sigil between elements
IFS='|' # specify separator between elements
set -f # disable glob expansion, so a * doesn't get replaced with a list of files
getNth() { shift "$(( $1 + 1 ))"; printf '%s\n' "$1"; }
getLast() { getNth "$(( $(length "$@") - 1 ))" "$@"; }
length() { echo "$#"; }
length $s # emits 4
getNth 0 $s # emits A
getNth 1 $s # emits B
getLast $s # emits D
s2='t1 t2 t3 t4'
set -f; IFS=' ' # IFS='\n' appears to be broken
for jar in ${s2}; do
set +f; unset IFS
echo "$jar" # restore globbing and field splitting at all whitespace
done
set +f; unset IFS # do it again in case $INPUT was empty
echo "done"
Sometimes it can be extremely convenient to have them and having a simple example would help. Also it might help to show some broken behavior of
IFS=\n
, which can be very surprising.