Syntax
This section covers shell syntax: how commands are parsed, how values are expanded, and how special characters are interpreted.
Topics
- Quoting - Single quotes, double quotes, escaping
- Redirection - Input/output redirection, here-documents
- Pipes - Pipelines and command chaining
- Expansion - Parameter, command, arithmetic, brace, tilde, glob
- Heredocs - Here-documents and here-strings
Expansion Order
Shell expansion happens in this order (per POSIX):
- Brace expansion -
{a,b,c},{1..10} - Tilde expansion -
~,~/path - Parameter expansion -
$var,${var:-default} - Command substitution -
$(cmd) - Arithmetic expansion -
$((expr)) - Field splitting - Using
IFS - Pathname expansion (globbing) -
*,?,[...] - Quote removal - Remove quotes after expansion
Source: src/scripting/expansion.f90
Command Structure
A simple command consists of:
[VAR=value...] command [arguments...] [redirections...]
Commands can be combined:
| Operator | Meaning |
|---|---|
cmd1 ; cmd2 | Sequential execution |
cmd1 && cmd2 | Execute cmd2 if cmd1 succeeds |
cmd1 || cmd2 | Execute cmd2 if cmd1 fails |
cmd & | Execute cmd in background |
cmd1 | cmd2 | Pipe stdout of cmd1 to stdin of cmd2 |
Grouping
# Subshell (separate environment)
(cmd1; cmd2)
# Command group (same environment)
{ cmd1; cmd2; }
Note: Brace groups require the final semicolon and spaces around braces.