- Article pages now have a discussion option at the bottom (moderated/captcha, but no registration needed)

FIXME work in progress…

Parsing and execution

Nearly everything in Bash grammar can be broken down to a so-called "simple command". That's why the only thing Bash has to finally 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 that 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 is 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:

  • The variable assignments affect the current shell environment.
    • This is what happens when you only enter a variable assignment at prompt.
    • Assignment to readonly variables causes an error and the command exits non-zero.
  • Redirections are performed, but do not affect the current shell environment.
    • that means, a > FILE without any command will be performed: the FILE will be created!
  • The command exits
    • with an exit code indicating the redirection error, if any
    • with the exit code of the last command substitution in the command parsed, if any
    • with the exit code 0 (zero) if no redirection-error happened and no command substitution was done

Otherwise, if a command name results:

  • The variables saved and parsed before are added to the environment of the executed command (and thus do not affect the current environment)
    • Assignment to readonly variables causes an error and the command exits non-zero.
    • Assignment errors in non-POSIX modes cause the enclosing commands (e.g. loops) to completely terminate
    • Assignment errors in (non-interactive) POSIX mode cause the complete script to terminate

The behavior regarding the variable assignment errors can be tested:

This one completely exits the script

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

echo PRE

# The following is an assignment error, since there can be no digit '9'
# for a number with the base '8'!
foo=$[8#9]

echo POST

This one only terminates 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 already parsed simple command contains no slashes, the shell attempts to locate and execute it:

  • shell functions
  • shell builtin commands
  • check own hash table
  • search along PATH

Since Bash Version 4, whenever a command search fails, the shell executes a shell function named command_not_found_handle() providing the commandline in question as arguments. This can be used to provide user-friendly messages or install software packages etc… Since this function runs, naturally, 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

Discussion

Enter your comment
 
syntax/grammar/parser_exec.txt · Last modified: 2013/05/24 13:10 by ormaaj
GNU Free Documentation License 1.3
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0