IFS stands for Internal Field Separator. It is an environment variable that defines a field separators. By default, space, tab, and newline are considered as field separators but you can change it in your script as per your need. It is mainly used in for loop to manipulate fields present in the list.
Consider an example in which an array element is having space. Without using IFS see what you will get in the output.
cars=(Toyota Honda Renault "Maruti Suzuki") for car in ${cars[*]} do echo $car done
Output of the above program
Toyota Honda Renault Maruti Suzuki
Actually, Maruti and Suzuki must come in one line but it is not coming. So with the help of IFS, you can solve this problem.
IFS=$'\n' cars=(Toyota Honda Renault "Maruti Suzuki") for car in ${cars[*]} do echo $car done
Output of the above program
Toyota Honda Renault Maruti Suzuki
Here, we have defined newline as a field separator to get Maruti Suzuki in one line.
#Bash Script to display usernames from /etc/passwd file IFS=$'\n': i=1 for user in `cat /etc/passwd` do if [ $i -eq 1 ] then echo $user fi if [ $i -eq 7 ] then i=1 else i=$((i+1)) fi done
Output of the above program
root daemon bin sys guest nobody
Here, we have defined newline and colon as a field separator.