Table of Contents

Words...

This article needs a review, it covers two topics (commandline splitting and word splitting) and mixes both a bit too much. But in general, it's still usable to understand this behaviour, it's "wrong but not wrong".

One fundamental principle of Bash is to recognize words in the commandline that was entered, or under some other circumstances like expanding a variable.

Splitting the commandline

Bash scans the commandline and splits it into words, usually to put the parameters you enter for a command into the right C-memory (the argv vector) to later correctly call the command. These words are recognized by splitting the commandline at the position of special characters, Space or Tab (manual defines them as blanks).
For example, take the echo program. It displays all its parameters separated by a space on the terminal. When you enter an echo command on your Bash prompt, Bash will do what told above: It will look for those special characters, and use them to determinate what are separate parameters.

You don't know what I'm talking about? I'm talking about this:

$ echo Hello little world
Hello little world

In other words, something you do (and Bash does) everyday. The characters where Bash splits the command line (SPACE, TAB - blanks) are recognized as told above.
There is no null-argument generated when you have 2 or more blanks in the commandline. A sequence of more blank characters is treated like one single blank character.
Let's see what I mean:

$ echo Hello little world
Hello little world

As promised: Bash splits the commandline at the position of a sequence of one or more blanks into words, then it calls echo with each word being a separate parameter to it. In this example, echo is called with exactly 3 parameters: "Hello", "little" and "world"!

Does that mean we can't echo more than one Space now? Of course not!
Bash treats the blanks special, but there is a way to tell Bash to not treat them special: Escaping and quoting.

Escaping a character means, to take away the special role of a character. Bash will literally use an escaped character as text, even if it's a special one. Escaping is done by preceeding the character with a backslash:

None of the above spaces will be used to split the commandline into words (one, infact, the space between echo and the rest - it's used to determinate what is the command and what should be its arguments). Thus, echo is called with exaclty one parameter: "Hello little world".

And as we're all lazy and want an easy life, Bash has a mechanism to "mass-escape" a complete area of the commandline: Quoting.
In the context of commandline-splitting, which this section is about, it doesn't matter which kind of quoting you use: weak quoting or strong quoting, both mask the blanks we talked about to not be special:

$ echo "Hello little world"
Hello little world

$ echo 'Hello little world'
Hello little world

What is it all about now? Well, for example imagine a program that expects a filename as parameter, like cat. Filenames can have spaces in them:

Word splitting

The first kind of splitting is done by Bash to parse the commandline into separate tokens. This is what was described above, it's a pure commandline parsing.

After the commandline has been split into words, Bash will perform expansions, if needed - variables that occur in the commandline need to be expanded (substituted by their value), for example. This is where the second type of word splitting comes in - several expansions finally undergo word splitting (but others do not).

Imagine you have a filename stored in a variable (variables will be explained elsewhere, just believe what you see):

MYFILE="test file"

When this variable is used, its occurance will be substituted by its content.

$ cat $MYFILE
cat: test: No such file or directory
cat: file: No such file or directory

Though this is another step where spaces make a hard life, the quoting is used again to influence it. Quotes also affect the word splitting:

$ cat "$MYFILE"
m00!

Example

Let's follow an unquoted command through these steps, assuming that the variable is set:

MYFILE="THE FILE.TXT"

and the first review is:

echo The file is named $MYFILE

The parser will scan for blanks and mark the relevant words ("splitting the commandline"):