ifs
Set the Internal Field Separator variable
TLDR
View the current IFS value
Change the IFS value
Reset IFS to default
Temporarily change the IFS value in a subshell
SYNOPSIS
IFS is an environment variable, not a command. Its usage involves setting its value.
Default Behavior (when IFS is unset or set to its default value):$ # Default: space, tab, newline
$ # Example: var=" a b
c "
$ for i in $var; do echo "[$i]"; done
Setting IFS to a custom value:$ IFS=':'
$ path_elements=$(echo "$PATH" | tr ':' '
')
$ # This example shows how IFS is used for splitting output of command substitution.
Temporarily changing IFS for a single command:$ IFS=',' read -r name age city <<< "John,30,New York"
Saving and restoring IFS:$ OLD_IFS="$IFS"
$ IFS=','
$ # ... perform operations ...
$ IFS="$OLD_IFS"
or using a subshell:$ (IFS=',' ; read -r a b c <<< "x,y,z"; echo "[$a] [$b] [$c]")
PARAMETERS
Default or Unset IFS
When IFS is unset or explicitly set to its default value (space, tab, newline), it splits on any sequence of these characters. Multiple delimiters are treated as a single delimiter, and leading/trailing delimiters are ignored.
Empty IFS (IFS=''
)
Setting IFS to an empty string completely disables word splitting. The entire string is treated as a single word. This is useful when you want to preserve all whitespace and newline characters in a variable or command output.
Single Character IFS (e.g., IFS=','
)
When IFS contains only one character, that character becomes the sole delimiter. All occurrences of this character will cause a split. Unlike the default behavior, multiple consecutive delimiters will result in empty fields, and leading/trailing delimiters will create leading/trailing empty fields.
Multiple Custom Characters IFS (e.g., IFS=':;'
)
If IFS contains multiple custom characters, each character acts as a delimiter. Similar to a single character IFS, multiple consecutive delimiters (even if they are different IFS characters) will generate empty fields, and leading/trailing delimiters will create empty fields.
DESCRIPTION
The term "ifs" often refers to IFS (Internal Field Separator), which is not a command but a crucial environment variable in Unix-like shells (like Bash, Zsh, Ksh).
IFS defines the characters used by the shell to perform word splitting. This process occurs during several shell operations, including:
- Parameter Expansion: When variables are expanded (e.g., `"$var"` prevents this, but `$var` allows it).
- Command Substitution: Output from `$(command)` or `` `command` `` is split.
- Arithmetic Expansion: Less common but can apply.
- Line Reading: Most notably, the read command uses IFS to split input lines into fields.
By default, IFS contains a space, a tab, and a newline character. Understanding and manipulating IFS is fundamental for robust shell scripting, especially when parsing structured data like CSV files or command outputs.
CAVEATS
- Global Effect: Changing IFS affects all subsequent word splitting operations in the current shell context. Always localize changes (e.g., within a subshell
(...)
or by saving and restoring its value) to prevent unintended side effects. - Subtleties of Default vs. Custom: The rules for how multiple delimiters are treated (as one vs. generating empty fields) and whether leading/trailing delimiters are ignored differ significantly between the default IFS behavior and when IFS is explicitly set to a custom value (even if that custom value includes space/tab/newline).
- Quoting Prevents Splitting: Enclosing variables in double quotes (e.g.,
"$var"
) prevents word splitting and pathname expansion (globbing), regardless of the IFS value. This is a common and highly recommended practice.
IMPACT ON THE <B><I>READ</I></B> COMMAND
The read command specifically uses IFS to determine how to split an input line into fields before assigning them to variables. For instance, `IFS=',' read -r var1 var2 var3` will split an input line by commas. If IFS is unset or default, `read` will split on whitespace. `IFS= read -r line` is a common idiom to read an entire line without any word splitting, preserving all whitespace.
DISABLING WORD SPLITTING
Setting `IFS=''` (an empty string) is the standard way to completely disable word splitting for expansions. This can be crucial when dealing with filenames or other strings that might contain spaces, tabs, or newlines that you want to treat as part of the data, not as delimiters.
BEST PRACTICES FOR <B><I>IFS</I></B>
- Always localize IFS changes using a subshell `(...)` or by saving and restoring its original value.
- Use `"$var"` to quote variables unless you explicitly intend for word splitting to occur.
- Be explicit about `IFS` when parsing structured data; do not rely on its default value if your delimiters are not space/tab/newline.
- For simple comma-separated or similar parsing, `IFS=',' read -r ...` is often cleaner than complex regex or external commands like `awk` for simple cases.
HISTORY
The concept of an Internal Field Separator has been a fundamental part of Unix shells since the early days of the Bourne shell (sh). It was designed to provide a flexible mechanism for parsing and processing text, a common task in shell scripting. Subsequent shells like Korn shell (ksh), Bash, and Zsh inherited and expanded upon this mechanism, making IFS a consistent and powerful feature across most modern Unix-like environments.