ifs
shell variable that controls word splitting
TLDR
SYNOPSIS
IFS=characters
DESCRIPTION
IFS, the Internal Field Separator, is a shell variable that holds the set of characters the shell uses to split expanded words into fields. It is not a command: it is read by the shell itself during field splitting, by the read builtin when it assigns words to variables, and when `"$*"` or `"${array[*]}"` is expanded.The default value is space, tab, and newline. Bash resets IFS to that value at startup, so scripts can rely on it unless something changed it.Field splitting distinguishes two kinds of characters in IFS. Space, tab, and newline are *IFS whitespace*: runs of them count as a single delimiter, and leading or trailing runs are discarded, which is why the default value never produces empty fields. Any other character in IFS delimits exactly one field, so two of them in a row yield an empty field.Splitting happens only on the results of unquoted parameter expansion, command substitution, and arithmetic expansion. Quoted expansions and literal text in the script are never split, and pathname expansion runs afterwards on the resulting fields.When `"$*"` is expanded inside double quotes, the positional parameters are joined with the first character of IFS. The same rule applies to `"${array[*]}"`, which makes IFS a convenient way to join array elements.
PARAMETERS
unset
Field splitting behaves as if IFS held the default space, tab, and newline.null (IFS=)
No field splitting is performed, and read stops stripping leading and trailing whitespace.IFS whitespace
Space, tab, and newline. Consecutive occurrences collapse into one delimiter and leading or trailing ones are ignored.other characters
Each occurrence delimits one field, so adjacent delimiters produce empty fields.
CAVEATS
Assigning IFS as a prefix to a builtin such as `IFS=, read -r a b` is a temporary assignment that applies to that command only, which is safer than a global change. This does not hold for special builtins in a POSIX-conforming shell, where the assignment can persist.Restoring with `IFS=$old_ifs` sets IFS to the empty string if it had been unset, which is not the same as unset. Inside a function, declaring `local IFS` is a cleaner way to scope the change.`IFS=$'\n'` uses `$'...'` quoting, which is a bash, zsh, and ksh extension and is not available in strict POSIX `sh`. Use a literal newline inside quotes for portability.A trailing delimiter does not create an extra field: splitting `a,b,` on a comma yields two fields, not three. Empty fields between two delimiters are kept, so `a,,b` yields three.Changing IFS globally affects every later expansion in the script, including ones inside functions and sourced files. Restore it as soon as possible.
HISTORY
IFS dates back to the Bourne shell written by Stephen Bourne at Bell Labs in the late 1970s and is standardized by POSIX. It is present in every Bourne-compatible shell, including bash, dash, ksh, and zsh, though zsh does not perform field splitting on unquoted expansions by default.
