The shell operates according to the following general steps:
* Some string is read from a source, be it a file, the standard input, or a command string (see [`Shell`(1)](../man1/Shell.md))
* The shell parses the input to an abstract syntax tree
* The shell performs various expansions and/or resolutions on the nodes
* The shell performs various type checks and syntactic checks
* The shell interprets the AST, evaluating commands as needed
* For each given command, the shell flattens all the string/list arguments
* For each given command, the shell records the applicable redirections
* Should a command be executed, the shell applies the redirections, and executes the command with the flattened argument list
* Should a command need waiting, the shell shall wait for the command to finish, and continue execution
Any text below is superceded by the formal grammar defined in the _formal grammar_ section.
## General Token Recognition
This section describes the general tokens the language accepts, it should be noted that due to nature of the language, some tokens are valid only in a specific context.
##### Bareword
String of characters that are not _Special_ or _Syntactic Elements_
##### Glob
String of characters containing at least one of `*?` in _bareword_ position
##### Single Quoted String
Any sequence of characters between two single quotes (`'`)
##### Double Quoted String
Any sequence of _Double Quoted String Part_ tokens:
* Barewords
* Single Quotes
* Variable References
* Evaluate expressions
* Escaped sequences
##### Variable Reference
Any sequence of _Identifier_ characters, or a _Special Variable_ follwing a `$`
##### Evaluate expression
Any expression following a `$` that is not a variable reference:
* Inline execution: A _syntactic list_ following a `$`:
* Dynamic evaluation: Any other expression following a `$`
##### Lists
Any two expressions joined by the Join operator (` ` [whitespace]), or a _variable reference_ referring to a list value
* Syntactic Lists: Any _list_ enclosed in parentheses (`(` and `)`)
##### Comments
Any text following a `#` in _bareword_ position, up to but not including a newline
##### Keywords
The following tokens:
*`for` in command name position
*`in` as a syntactic element of a `for` expression
Any initial path segment starting with the character `~` in _bareword_ position, Optionally followed by a _bareword_ for the username
## Redirections
The shell can create various redirections to file descriptors of a command before executing it, the general syntax for redirections is an optional file descriptor, followed by a redirection operator, followed by a destination.
There are four redirection operators corresponding to various file descriptor open modes: `Read`, `Write`, `WriteAppend` and `ReadWrite`, respectively `<`, `>`, `>>` and `<>`.
A special syntactic element `&fd` can reference a file descriptor as a destination.
Redirections take two main forms, Read/Write redirections, and fd closure redirections.
* Allowed destinations: the special "close" reference `&-`
#### Examples
```sh
# Redirect the standard error to a file, and close the standard input
$ 2> foo 1>&-
# Redirect a file as read-write into the standard input
$ 1<>foo
# Redirect the standard output to /dev/null
$ >/dev/null
```
## Expansions
The shell performs various expansions, in different stages.
* Glob Expansion: Globs shall be expanded to a list.
* Variable Expansion: Variables shall be expanded preserving their types.
* Juxtaposition Expansion: Juxtapositions shall be expanded as list products.
* Other expansions: Tildes, Evaluate expressions, etc. shall be expanded as needed.
### Juxtapositions
Any two expressions joined without any operator are considered to be in a Juxtaposition, with the resulting value being the list product of two expressions.
For instance, `(1 2)(3 4)` shall be evaluated to `(13 14 23 24)` by calculating the list product of the two expressions `(1 2)` and `(3 4)`.
### Tildes
Any bareword starting with a tilde (`~`) and spanning up to the first path separator (`/`) - or EOL - is considered to be a tilde expansion with the text between the tilde and the separator being the _username_, which shall be expanded to a single string containing the home directory of the given _username_ (or the current user if no username is provided).
### Evaluate
Evaluate expressions take the general form of a dollar sign (`$`) followed by some _expression_, which is evaluated by the rules below.
- Should the _expression_ be a string, it shall be evaluated as a dynamic variable lookup by first evaluating the string, and then looking up the given variable.
- Should the _expression_ be a list or a command, it shall be converted to a command, whose output (from the standard output) shall be captured, and split to a list with the shell local variable `IFS` (or the default splitter `\n` (newline, 0x0a)). It should be noted that the shell option `inline_exec_keep_empty_segments` will determine whether empty segments in the split list shall be preserved when this expression is evaluated, this behaviour is disabled by default.
A `Command` is a single simple command, containing arguments and redirections for a single program, or a compound command containing a shell control structure. The shell can evaluate a sequence of commands, a conditional relation between commands, or various semantic elements composed of commands and intrinsics.
Short-circuiting command evaluation, will continue down the chain if any command fails.
It should be noted that `And` chains bind more tightly than `Or` chains, so an expression of the form `C1 && C2 || C3` is understood as "evaluate `C1`, if successful, evaluate `C2`, if not successful, evaluate `C3`".
An `if` expression evaluates either the _then clause_ or (if available) the _else clause_, based on the exit code of the _condition_; should the exit code be zero, the _then clause_ will be executed, and if not, the _else clause_ will.
The Shell shall cancel the for loop if two consecutive commands are interrupted via SIGINT (\^C), and any other terminating signal aborts the loop entirely.
Subshells evaluate a given block in a new instance (fork) of the current shell process. to create a subshell, any valid shell code can be enclosed in braces.
###### Examples
```sh
# Run a block of code in the background, in a subshell, then detach it from the current shell