Saturday, February 2, 2013

Internal field separator (IFS) for shell scripts

Internal field separator (IFS)

By default, Unix/Linux shell scripts takes space as field separator to process input text. This may work in some scenarios but not all case. By setting IFS variable to some character(s) will take care of input parsing using that.

For example, while looping through the directory file names in Linux, spaces in file name will divert the actual outcome what you are expecting. In that case use the below variable to set new line and tab characters to be a field separator and done it.
   IFS="$(printf '\n\t')"
   for fileItem in ./*
   do
        # do your stuff ...
   done

No comments:

Post a Comment