Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
syntax:grammar:parser_exec [2013/04/14 12:38] thebonsai Don't tread version 4 special - it has been around for a long time now |
syntax:grammar:parser_exec [2019/10/31 16:18] (current) ersen change ancient arithmetic expansion syntax |
||
---|---|---|---|
Line 5: | Line 5: | ||
{{keywords>bash shell scripting syntax language behaviour executing execution}} | {{keywords>bash shell scripting syntax language behaviour executing execution}} | ||
- | Nearly everything in [[syntax:basicgrammar | 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. | + | Nearly everything in [[syntax:basicgrammar | 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 ===== | ===== Simple command expansion ===== | ||
+ | <div center round info 60%> | ||
+ | * http://lists.gnu.org/archive/html/bug-bash/2013-01/msg00040.html | ||
+ | * http://lists.research.att.com/pipermail/ast-developers/2013q2/002456.html | ||
+ | </div> | ||
This step happens after the initial command line splitting. | 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**): | The expansion of a simple command is done in four steps (interpreting the simple command **from left to right**): | ||
- | - The words that the parser has marked as **variable assignments** and **redirections** are saved for later processing. | + | - 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'' | * variable assignments precede the command name and have the form ''WORD=WORD'' | ||
* redirections can appear anywhere in the simple command | * redirections can appear anywhere in the simple command | ||
- | - The rest of the words is [[syntax:expansion:intro| 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**. | + | - The rest of the words are [[syntax:expansion:intro| 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**. |
- [[syntax:redirection | Redirections]] are performed. | - [[syntax:redirection | Redirections]] are performed. | ||
- The text after the ''='' in each variable assignment undergoes [[syntax:expansion:tilde | tilde expansion]], [[syntax:pe | parameter expansion]], [[syntax:expansion:cmdsubst | command substitution]], [[syntax:expansion:arith | arithmetic expansion]], and quote removal before being assigned to the variable. | - The text after the ''='' in each variable assignment undergoes [[syntax:expansion:tilde | tilde expansion]], [[syntax:pe | parameter expansion]], [[syntax:expansion:cmdsubst | command substitution]], [[syntax:expansion:arith | arithmetic expansion]], and quote removal before being assigned to the variable. | ||
Line 22: | Line 26: | ||
If **no command name** results after expansion: | If **no command name** results after expansion: | ||
* The variable assignments affect the **current shell** environment. | * The variable assignments affect the **current shell** environment. | ||
- | * This is what happens when you only enter a variable assignment at prompt. | + | * This is what happens when you enter only a variable assignment at the command prompt. |
* Assignment to readonly variables causes an error and the command exits non-zero. | * Assignment to readonly variables causes an error and the command exits non-zero. | ||
* Redirections are performed, but do not affect the current shell environment. | * Redirections are performed, but do not affect the current shell environment. | ||
Line 28: | Line 32: | ||
* The command exits | * The command exits | ||
* with an exit code indicating the redirection error, if any | * 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 of the last command-substitution parsed, if any |
- | * with the exit code 0 (zero) if no redirection-error happened and no command substitution was done | + | * with exit code 0 (zero) if no redirection error happened and no command substitution was done |
Otherwise, if a command name results: | 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) | + | * The variables saved and parsed 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 to readonly variables causes an error and the command exits with a non-zero error code. |
* **Assignment errors** in non-POSIX modes cause the //enclosing commands (e.g. loops) to completely terminate// | * **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// | + | * **Assignment errors** in (non-interactive) POSIX mode cause //the entire script to terminate// |
- | The behaviour regarding the variable assignment errors can be tested: | + | The behavior regarding the variable assignment errors can be tested: |
+ | <div center round info 60%>http://lists.gnu.org/archive/html/bug-bash/2013-01/msg00054.html</div> | ||
- | **__This one completely exits the script__** | + | **__This one exits the script completely__** |
<code> | <code> | ||
#!/bin/sh | #!/bin/sh | ||
Line 46: | Line 51: | ||
echo PRE | echo PRE | ||
- | # The following is an assignment error, since there can be no digit '9' | + | # The following is an assignment error, since there is no digit '9' |
- | # for a number with the base '8'! | + | # for a base eight number! |
- | foo=$[8#9] | + | foo=$((8#9)) |
echo POST | echo POST | ||
Line 54: | Line 59: | ||
- | **__This one only terminates the enclosing compound command (the ''{ ...; }''):__** | + | **__This one terminates only the enclosing compound command (the ''{ ...; }''):__** |
<code> | <code> | ||
#!/bin/bash | #!/bin/bash | ||
Line 63: | Line 68: | ||
# The following is an assignment error! | # The following is an assignment error! | ||
# The "echo TEST" won't be executed, since the { ...; } is terminated | # The "echo TEST" won't be executed, since the { ...; } is terminated | ||
- | { foo=$[8#9]; echo TEST; } | + | { foo=$((8#9)); echo TEST; } |
echo POST | echo POST | ||
Line 71: | Line 76: | ||
===== Simple command execution ===== | ===== Simple command execution ===== | ||
- | If a already parsed simple command contains no slashes, the shell attempts to locate and execute it: | + | If a parsed simple command contains no slashes, the shell attempts to locate and execute it: |
* shell functions | * shell functions | ||
* shell builtin commands | * shell builtin commands | ||
Line 77: | Line 82: | ||
* search along ''PATH'' | * 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, ...). | + | 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 | FIXME to be continued |