IFS (Field Separator)

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

The field separator used in a lot of function to parse a string is the variable $IFS.

$IFS defaults to whitespace (space, tab, and newline)

When passing a stream to a function, the parameters will be parsed with this value before being assigned.

Advertising

3 - Example

#!/bin/bash
output_args()
{
  for arg
  do
    echo -e '('"$arg"')'
  done
}
 
var=$'hello Nico\nHello Nico!'
echo "IFS is a return \n"
IFS=$'\n'
output_args $var
 
echo IFS is a space
IFS=' '
output_args $var
IFS is a return \n
(hello Nico)
(Hello Nico!)
IFS is a space
(hello)
(Nico
Hello)
(Nico!)