1

Bash manual says that Expansion is performed on the command line after it has been split into words.. However, in the next sentence it states that there are seven kinds of expansion performed and word splitting is the sixth one. As I understand, those two are different word splittings. What is a difference between those two word splittings?

3

There is only one word splitting, and it happens after brace expansion, tilde expansion, parameter and variable expansion, command substitution, and arithmetic expansion, but before pathname expansion.

(Hint: word splitting splits words into more words. You are confusing word splitting with the initial splitting of the command line into tokens.)

  1. First, the command line is split into words (or "tokens") separated by the metacharacters |, &, ;, (, ), <, >, <space>, and <tab>.

  2. Then, the words representing the command line are parsed into lists, pipelines and simple commands.

  3. Expansions happen when each simple command is considered for execution.

    • After completing brace expansion, tilde expansion, parameter and variable expansion, command substitution, and arithmetic expansion, the shell goes through the unquoted results of expansions and splits them into words, this time separated by the characters in $IFS (not the metacharacters which were used to split the command line).

    • Finally, the shell goes again through the list of unquoted expansion results and performs pathname expansion.

Read all the gory details in the POSIX specification of the shell command language. (POSIX describes a somewhat simpler shell; Bash implements some extensions, but it should be easy to see how they fit.)

To answer the question as asked: the initial splitting of the command line into tokens separated by shell metacharacters; word splitting splits the results of expansion into words separated by characters in $IFS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.