Bash - IFS (Field Separator)

Bash Liste Des Attaques Ovh

About

The field separator is a set of character that defines one or more field separator that separates (delimit) field (word) in a string.

This NOT a atomic string separator but a set of single-character string separator ie

IFS="DELIM"

will define 5 characters separators, respectively: D, E, L, I and M

It's defined in the IFS variable 1)

When passing a string to a function, the parameters will be parsed with this value before being assigned. See What is Word Splitting (or Word expansion) in Bash ?

The backslash statement break character is the equivalent of an IFS character ie:

command \
  arg1 \
  arg2

is equivalent to

command "$IFS" arg1 "$IFS" arg2

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!)

Management

Print

As the IFS character has a lot of chance to be a non-printing character, don't forget quote it with an echo command

Example:

echo "$IFS"
  • Proof with sed that will replace the tab \t into an arrow
IFS=$'\t'; echo "$IFS"  | sed 's/\t/→/g;'

Default

IFS default value is the following whitespace character

You can find it out with the following command that uses cat -vte to show the whitespace characters

 echo "$IFS" | cat -vte
^I$
$

where we see

  • a space
  • ^I which means a tab
  • $ which means a end of line





Discover More
Bash Liste Des Attaques Ovh
Bash - (Argument|Positional Parameter)

An argument is a parameter given: to a command to a function or to the bash shell They are referenced by position. A positional parameter is a parameter denoted: by one or more digits, ...
Bash Liste Des Attaques Ovh
Bash - How to pass arguments that have space in their values ?

This article shows you how to pass arguments that have space characters in their values. Passing several arguments to a function that are stored as a string may not work properly when you have space...
Bash Liste Des Attaques Ovh
Bash - Read (Builtin Command) that capture a line

Read is a bash builtin command and read: by default one line or a number of characters (by option) from: the standard input, or from the file descriptor fd supplied as an argument to the...
Bash Liste Des Attaques Ovh
Bash - How to parse a CSV (Or properties files)

How to parse a CSV or property (ini) in bash where: the first FOR iterate over a list of ini file in the current directory. the while read: reads a line of ${FILE} until it finds an EOF parse...
Bash Liste Des Attaques Ovh
How to use the Array Variable in Bash?

Bash provides a one-dimensional array variables. See also: When calling a function, quote the variable otherwise bash will not see the string as atomic. The separator is variable $IFS. There is: ...
Bash Liste Des Attaques Ovh
Sh - String Variable

String Variable, see also character string in bash. When calling a function, quote the variable otherwise bash will not see the string as atomic but as an array Sh with Bash “” The...
Card Puncher Data Processing
Shell - (Word | Token | Field)

Shell - (Word | Token | Field) In a bash pipeline, the pipeline message is known as: the word the field This is a sequence of characters considered as a single unit by the shell. The first...
Bash Liste Des Attaques Ovh
What is Word Splitting (or Word expansion) in Bash ?

word splitting is an expansion that splits a string into words. When passing a string to a function, the parameters will be parsed with the IFS value before being assigned. The shell scans the results...



Share this page:
Follow us:
Task Runner