internal field separator shell script
Internal filed separator is a special shell variable symbolized as IFS. The default value assigned to IFS is space, tab and new line. it can be used to tokenized a sentence. You can check default value by “echo $IFS” command in linux terminal. after running this command we will terminal prompt with new line on ubuntu os.
Let us make a sentence “Linux is free and open source operating system. it is good os.” open your terminal and type below command.
alen@Alen-Vostro-1550:~$ set Linux is free and open source operating system, it is good os.
alen@Alen-Vostro-1550:~$ echo $1 $2 $3 $4 $5 $6 $7 $8 $9
output will be :
Linux is free and open source operating system, it
Here $1 will print Linux and $2 will print is and so on. here set command assign this value with the help of IFS variable because one of the IFS value is space.
we can change the default value of IFS to change the set command behavior. let us write a sample.txt file with blow content.
Linux is free and open source operating system, it is good os.
(2) save below code as script.sh
for line in $(cat sample.txt)
(3) Make script.sh executable by
alen@Alen-Vostro-1550:~$ chmod +x script.sh
(4) Run the script
alen@Alen-Vostro-1550:~$ sh script.sh
The output will be:
Linux is free and open source operating system
it is good os
Here in script.sh we have changed the IFS default value to , that is why we are getting “It is good os” in second line. hence in this way we can tokenize the sentence according to our need.