🐧 Linux / Shell Scripting
Advanced
What is the significance of IFS in bash?
Answer
IFS (Internal Field Separator) determines how bash splits words during word splitting and how read splits input into fields. Default value is space+tab+newline ($' \t\n'). When bash performs unquoted variable expansion, it splits on IFS characters. Change IFS to parse CSVs: IFS=',' read -ra fields <<< "a,b,c". Parse a colon-delimited string: IFS=':' read -ra parts <<< "$PATH". Temporarily change IFS: old=$IFS; IFS='|'; ...; IFS=$old. Critical: always reset IFS after changing, or use a subshell. The safety idiom while IFS= read -r line sets IFS to empty to prevent stripping of leading/trailing whitespace from each line — essential for correctly reading files line by line.