Interview › Scripting (Bash, Groovy)
What does IFS do, and why set it when reading lines? [Basic]
Answer
IFS is the Internal Field Separator. Bash uses it for word splitting and read field splitting. When reading whole lines, I often set IFS= for that read command so leading and trailing whitespace is preserved.
Technical explanation
The default IFS includes space, tab, and newline, which can unintentionally trim or split input.
Setting IFS locally for one read avoids changing global shell behavior.
IFS can also be used intentionally to split delimited data, such as CSV-like simple fields, but CSV with quoting needs a real parser.
Hands-on example
line=" alpha beta "
while IFS= read -r value; do
printf '<%s>
' "$value"
done <<< "$line"
IFS=: read -r user _ uid _ _ home shell < <(getent passwd "$USER")
echo "$user $uid $home $shell"Preparing for an interview?
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- How do you write and call a function in Bash? [Basic]
- How do you write a for loop over a list and a C-style for loop? [Basic]
- How do you pass arguments to a function and return a value or status? [Basic]
- What is local scope in a Bash function? [Basic]
- What are the common file test operators (-f, -d, -e, -r, -w, -x)? [Basic]
- What is OPTARG, and how does getopts handle options with values? [Intermediate]
- What is the difference between [ ], [[ ]], and (( )) in Bash? [Basic]
- What is a here-string (<<<), and how does it differ from a here-doc? [Intermediate]