Table of Contents

FIXME work in progress…

Parsing and execution

Nearly everything in Bash grammar can be broken down to a "simple command". The only thing Bash has to expand, evaluate and execute is the simple command.

Simple command expansion

This step happens after the initial command line splitting.

The expansion of a simple command is done in four steps (interpreting the simple command from left to right):

  1. The words the parser has marked as variable assignments and redirections are saved for later processing.
    • variable assignments precede the command name and have the form WORD=WORD
    • redirections can appear anywhere in the simple command
  2. The rest of the words are expanded. If any words remain after expansion, the first word is taken to be the name of the command and the remaining words are the arguments.
  3. Redirections are performed.
  4. The text after the = in each variable assignment undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable.

If no command name results after expansion:

Otherwise, if a command name results:

The behavior regarding the variable assignment errors can be tested:

This one exits the script completely

#!/bin/sh
# This shell runs in POSIX mode!

echo PRE

# The following is an assignment error, since there is no digit '9'
# for a base eight number!
foo=$((8#9))

echo POST

This one terminates only the enclosing compound command (the { …; }):

#!/bin/bash
# This shell runs in native Bash-mode!

echo PRE

# The following is an assignment error!
# The "echo TEST" won't be executed, since the { ...; } is terminated
{ foo=$((8#9)); echo TEST; }

echo POST

Simple command execution

If a parsed simple command contains no slashes, the shell attempts to locate and execute it:

As of Bash Version 4, when a command search fails, the shell executes a shell function named command_not_found_handle() using the failed command as arguments. This can be used to provide user friendly messages or install software packages etc. Since this function runs in a separate execution environment, you can't really influence the main shell with it (changing directory, setting variables).

FIXME to be continued

See also