The Meaning of IFS in Bash Scripting on Linux


Introduction

In Bash scripts on Linux, the "IFS" (Internal Field Separator) variable plays an important role in controlling how fields in a string are separated. IFS defaults to a space, tab, and newline character, which means that, by default, fields in a string are separated by any combination of these characters. However, the IFS value can be changed to meet the specific needs of a script. In this article, we will explore the meaning of IFS in Bash scripting and how it can be used in various scenarios.

Linux

IFS is a special variable in Bash which is used to control the field separator for hyphenation and line parsing. By default, IFS is set to a space, a tab, and a newline character, which means that fields in a string are separated by any combination of these characters. For example, if the string "hello world" is passed to a script, the two fields in the string will be "hello" and "world", separated by a space.

IFS can be changed to any string, allowing for more flexibility in parsing the fields in a string. For example, if IFS is set to ",", fields in a string will be separated by commas. This can be useful when working with Comma Separated Values ​​(CSV) files, where each line in the file represents a record and the fields are separated by commas.

Changing IFS

IFS can be changed by assigning a new value to the variable. For example, to change IFS to a comma, use the following command −

$ IFS=","

It is important to note that changing the IFS value will only affect the current shell session. If you want the change to persist across sessions, you will need to set the IFS value in your “.bashrc” or “.bash_profile” file.

Using IFS in Word Splitting

IFS can be used in hyphenation to control how fields in a string are separated. The read built-in command can be used in conjunction with IFS to read fields from a string and assign them to variables. For example, the following command can be used to read fields from a CSV file and assign them to variables −

IFS=","
while read -r field1 field2 field3; do
   echo "Field 1: $field1"
   echo "Field 2: $field2"
   echo "Field 3: $field3"
done < input.csv

In this example, the while loop reads each line of the “input.csv” file and assigns the fields to the variables field1, field2, and field3, respectively. The “-r” option is used to prevent backslashes from being treated as escape characters.

Using IFS in Line Parsing

IFS can also be used in line parsing to control how fields in a string are separated. The cut command can be used in conjunction with IFS to extract specific fields from a string. For example, the following command can be used to extract the first and third fields of a string −

IFS=":"
string="field1:field2:field3"
fields=$(cut -f1,3 -d "$IFS" <<< "$string")
echo $fields
# Output : field1 field3

In this example, the cut command is used to extract the first and third fields of the string, using the IFS value as the field delimiter. The “-f” option is used to specify the fields to extract and the “-d” option is used to specify the delimiter to use. The output of this command will be "field1 field3", with fields separated by a space, since IFS is configured with a colon.

Using IFS in Array Manipulation

IFS can also be used in array manipulation to control how fields in a string are separated. The IFS variable can be used to split a string into an array using the read command. For example, the following command can be used to split a string into an array −

IFS=":"
string="field1:field2:field3"
read -a array <<< $string
for element in "${array[@]}"
do
   echo $element
done
# Output : 
# field1
# field2
# field3

In this example, the read command is used to split the string into an array using the IFS value as the field delimiter. The “-a” option is used to specify that the input should be treated as an array. The for loop is used to iterate over the elements of the array and print them.

Conclusion

In Bash scripts, the "IFS" variable plays an important role in controlling how fields in a string are separated. By default, IFS is set to a space, a tab, and a newline character, which means that fields in a string are separated by any combination of these characters. However, the IFS value can be modified to meet the specific needs of a script. By changing the value of IFS, it can be used in word splitting, row parsing, and array manipulation. This provides a high degree of flexibility in parsing fields in a string, making it an essential tool for any Bash script.

Updated on: 20-Jan-2023

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements