The IFS is an abbreviation for Internal Field Separator which is an environment variable in the shell to determine the separator. The $IFS specifies how the words on the command prompt are delimited. By default, field separators (IFS) are the space, tab, and newline but we can alter this in our script to match the requirements. It is mostly used for loops to manipulate the elements in the specified list. We use the $IFS variable in our Bash script to split the string because Bash does not include a built-in mechanism for string splitting.
Example 1: Bash Script without $IFS Operator
Consider the following Bash script where the elements are separated with the blank space. Here, we are not using the $IFS operator to see the output of the normal Bash script. We create the “Bash.sh” file and then open it to write the program to print the string on the Bash shell. We begin with the declaration of the variable which is titled “$MySentence” and specify this variable with the long string. We have various words in the string which are split by the space. Then, we call the foreach loop to print each word of the “$MySentence” string. The foreach loop is set with the “MyWords” object which holds the iterated word from the string. After that, we echo the iterated words from the string with the echo command which is deployed between the do and done statement.
MySentence="Bash is an effective programming"
for MyWords in $MySentence
do
echo "$MyWords"
done
Open the terminal with the “Ctrl+Alt+T” shortcut key and compile the “bash.sh” file with the Bash command. The displayed words in the shell are in a separate line because the space, tabs, and the new line are considered field separators in Bash.
Example 2: Bash Script with the $IFS Operator
We separate the words in the specified string without using the $IFS operator. We can use the $IFS operator with the desired character. If we alter the default IFS, it’s a wise strategy to store the old one in a variable. Once we complete our required tasks, we can reassign the original character to IFS. Let’s have the Bash script where the $IFS operator is employed to separate the words.
Here, we specify the string as the name of persons which are separated by the character colon “:” inside the “Persons” variable. Then, we store the original IFS value in the “Pre_ifs” attribute. Next, we declare the IFS variable where the colon “:” is set as a delimiter. The IFS variable is used as a word token which is used for the for loop. We employ the foreach loop to traverse the string of the “Persons” variable in split form and pass all the string elements to the “Per” object of the for-each loop. Then, we use the echo statement within the do and done commands to display the separated strings.
Persons=Alex:Thomas:Kyle:Jenny
Pre_ifs="$IFS"
IFS=":"
for Per in $Persons
do
echo The name of Persons are $Per
done
Compile it with the Bash script file in the terminal to see the output of using the IFS operator. When the Bash file of the IFS operator is executed, it separates each string in the following format:
Example 3: Bash Script with the $IFS Operator to Display the Username from the File
Now, we use the $IFS operator in the Bash script to display the name of the users from the “/etc/passwd” file. We use the $IFS operator here which separates each username in the shell. The following Bash script begins with the declaration of the “IFS” variable which is set with the “$’\n’” character and a colon “:”. The $’\n’ character is a newline character that is used for the escape sequence. The usernames are separated via provided IFS.
After this, we have a variable “x” whose value is assigned as “1”. Then, we use a cat command with the “/etc/passwd” file which passes the username to the “user” object of the foreach loop to iterate over all the users of the “/etc/passwd” file. The “do” and “done” commands are specified with the nested “if” conditional statements to echo the users.
x=1
for user in `cat /etc/passwd`
do
if [ $x -eq 1 ]
then
echo $user
fi
if [ $x -eq 7 ]
then
x=1
else
x=$((x+1))
fi
done
We execute the Bash script on the following terminal screen which outputs the users of the “/etc/passwd” by a specified field separator:
Example 4: Command-Line Bash Script with $IFS
The IFS is a specific shell variable so we can use it directly in the command line. Here, we have a shortcut to using the IFS operator. Instead of creating a separate Bash script file, we open the terminal and define the “IFS” variable. The “IFS” variable is set with the hyphen “-” delimiter. Then, we echo a “Hello-Geeks” string which is separated by a field separator hyphen “-”. Then, we use the read command to define the variables “var1” and “var2” and echo these variables separately.
echo "Hello-Geeks!" | (read var1 var2; echo $var1; echo $var2)
The output retrieves the Hello string and the “Geeks” string separately. Hence, the $IFS accepts the delimiters individually.
Example 5: Splitting the User-Defined String in Bash Script with IFS
We utilized the $IFS operator in the previous Bash script to demonstrate the functionality. Now, we use an IFS operator to set the separator for the string of words that are defined by the user in the terminal. We give a read command with the “-p” flag. This command prints the “Enter the string with space” statement. Then, the entered string is initialized in the “StringIs” variable. The IFs operator is set with the space delimiter to divide the strings.
Next, we have the “read -ra ADDR” command which reads “$StrinIS” as an array of tokens which are split by IFS. The “-r” option specifies that backslash “\” is not an escape character, it’s a character. The “-a” option specifies that the words are separated by $IFS allocated to the array’s sequential index which starts at zero. After that, we access the string elements of the array by traversing it through the foreach loop.
read -p "Enter the string with space: " StringIS
IFS=' '
read -ra ADDR <<<"$StringIS"
for i in "${ADDR[@]}";
do
echo "$i"
done
The string that is entered by the user contains the empty spaces whose execution is obtained as follows:
Conclusion
This editorial discovered the method of separating a token in Bash through the $IFS by implementing the Bash scripts. We also used the $IFS in the command prompt to divide the string. This is a very simple and short way to accomplish the separation of the string. Although, the shell uses each IFS character as a delimiter and divides the findings of the other iterations into words.