Word Splitting Using Bash

Shell Scripting

The results of parameter and arithmetic expansions, as well as command substitution, are subjected to word splitting if they were not quoted:

$ var="this is a multiword value"

$ sa $var "$var"

:this:

:is:

:a:

:multi-word:

:value:

:this is a multi-word value:


Word splitting is based on the value of the internal field separator variable, IFS. The default value of IFS contains the whitespace characters of space, tab, and newline (IFS=$’ \t\n’). When IFS has its default value or is unset, any sequence of default IFS characters is read as a single delimiter.

$ var='         spaced out     '

$ sa $var

:spaced:

: out:

If IFS contains another character (or characters) as well as whitespace, then any sequence of whitespace characters plus that character will delimit a field, but every instance of a nonwhitespace character delimits a field:

S IFS=' :'

$ var="qwerty : uiop : :: er " ## : :: delimits 2 empty fields

$ sa $var

:qwerty:

:uiop:

::

::

:er:

If IFS contains only nonwhitespace characters, then every occurrence of every character in IFS delimits a field, and whitespace is preserved:

$ IFS=:

$ var="qwerty : uiop : :: er "

$ sa $var

:qwerty :

: uiop :

: :

::

: er :

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.