In Bash Scripting, the IFS is the Internal Field Separator or token delimiter that is used for splitting the strings. It tells the system how the string will be printed on the screen. By default, the space, tab, and new line are the field separators of the IFS, but this can be modified for white spaces such as ”:”, “,” and other characters. It is mostly used in the loops to manipulate the output of the strings, as there is no built-in mechanism in the bash script to perform such a task.
This article will enlighten the meaning and working of the IFS in bash script files.
- IFS in Bash Script File
- Example 1: Executing Simple Bash Script Without IFS
- Example 2: Executing Bash Script with “IFS”
- Example 3: Executing Bash Script with IFS From User Input
- Example 4: Executing Bash Script with IFS Through Positional Parameters
- Example 5: Executing Script with IFS=”-” Through Command Line
IFS in Bash Script File
To use the IFS in the bash script, various examples are implemented in the below guide.
Example 1: Executing Simple Bash Script Without IFS
Let’s execute the bash script file without IFS to understand the concept.
#!/bin/bash
String="Executing Bash Script File"
for word in $String
do
echo "$word"
done
- A long string is defined separated by spaces.
- Then, a for-each loop to iterate each word one by one.
- Then, the “do” statement stores it in the “$word” variable and prints it on the screen:
Now, run the bash script file using the “bash” command:
$ bash ./script.sh
Each word has been iterated and printed on the screen as the default field separators are spaces.
Example 2: Executing Bash Script with “IFS”
Now, let’s execute the bash script file with IFS and a string of authors that have a field separator of the coma “,”:
#!/bin/bash
Authors_String=Henry,Joseph,Johnsons,Milton
pre_ifs="$IFS"
IFS=","
for Author in $Authors_String
do
echo $Author
done
The script is defined as
- A long string of Authurs is defined separated by commas.
- Then, store old IFS in the “pre_ifs” variable so that it will be reassigned once the task is completed.
- Defining the value of IFS=”,”.
- Then, a for-each loop to iterate each author name one by one.
- Then the “do” statement is storing it in the “$Authur” variable and printing on the screen.
Save and compile the bash script file for the results through the below command:
$ bash ./script.sh
Each field of the string has been separated.
Example 3: Executing Bash Script with IFS From User Input
To take the string from the user input, run the below script in the file:
#!/bin/bash
read -p "Enter the string with space: " String
IFS=' '
read -ra ADDR <<<"$String"
for i in "${ADDR[@]}";
do
echo "$i"
done
The script is defined as
- “read ” property storing the user input in “String” variable by printing message.
- Defining the IFS =” ” with space
- “read -ra ADDR” to read the string as an array. Here, the “r” flag tells the system that “\n” is not an escape character. While the “a” flag tells that the separated words are allocated to the array with specified indexes.
- Then, a for-each loop to iterate each word one by one.
- Then the “do” statement is storing it in the “$i” variable and printing on the screen:
Compile and run the above script through the below-mentioned command:
$ bash ./script.sh
The user string has been iterated.
Example 4: Executing Bash Script with IFS Through Positional Parameters
The “[email protected]” and “$*” holds the list of the passed arguments, which are known as positional parameters. Let’s use them in the following script:
#!/bin/bash
IFS='|'
echo " Parameter Through \[email protected] are : [email protected]"
echo " Parameters Through \$* are : $*"
The script is defined as
- Set the value of IFS=“|” (OR Operator).
- “echo” statements printing list of arguments in “[email protected]” and “$*”.
Save and compile the file by passing the arguments:
$ bash ./script.sh argument1 argument2 argument3
The arguments are printed.
The output is described as:
- “[email protected]” expanded as “$1”, “$2”, “$3”….$n.
- “$*” expanded as “$1y”, “$2y”, “$3y”…..$n here y is the field separator because IFS act as a field separator or token delimiter
Example 5: Executing Script with IFS=”-” Through Command Line
The user can also use the script parameters through the command line for using the IFS. in the given command the IFS is specified with a hyphen(-) and a string in the echo statement combined with the pipe command. The next part is separating the word in the variables with a field separator of “–” and printing it on the screen:
$ IFS="-"
echo "Welcome-to-itslinuxfoss!" | (read var1 var2 var3; echo $var1; echo $var2; echo $var3)
Each word “welcome”, “to”, and “itslinuxfoss!” is separated and printed on the screen.
Conclusion
In the bash script file of Linux, the IFS is the field separator or token delimiter utilized in the string splitting. It is used for special escape characteristics such as commas “,” spaces “ ”, colon “:” etc. This write-up has illuminated the meaning of IFS and in bash script with various examples.