Skip to content
Snippets Groups Projects
Commit e742b435 authored by Thomas Junier's avatar Thomas Junier
Browse files

rewrote Loop section to better differentiate keywords fromplaceholders

parent f14c81e7
Branches
No related tags found
No related merge requests found
......@@ -5,11 +5,6 @@ geometry:
colorlinks: on
---
General
=======
* "some X" means those X that seem most relevant to programming
Operation
=========
......@@ -154,7 +149,7 @@ The result of most _unquoted_ expansions is split using the characters in
rules](https://www.gnu.org/software/bash/manual/html_node/Word-Splitting.html#Word-Splitting)
are more complex).
**Does not occur** in: `"${}"`, `"$()"`, `$(())`, `case`, `(())`, `[[]]`, `=`.
**Does not occur** in: `""`, `$(())`, `case`, `(())`, `[[]]`, `=`.
Filename Expansion
------------------
......@@ -168,74 +163,129 @@ Quote Removal
Removes all _unquoted_ `\` `'` `"` that did not result from an
expansion.
----
Redirections
============
Parameters
==========
(Details: ```man bash, / ^REDIR```)
Assignment
----------
There may be more than one, and order matters.
```bash
var=value # no spaces!
```
\begin{tabular}{p{2.0cm}p{6cm}}
\textbf{operator} & \textbf{behaviour} \\
\verb+[m]<file+ & open \texttt{file} for reading on fd \texttt{m} \\ % >
\verb+[n]>[|]file+ & open \texttt{file} for writing on fd \texttt{n}; \texttt{>|} ignores \texttt{noclobber} \\
\verb+[n]>>file+ & open \texttt{file} for appending on fd \texttt{n} \\
\verb+&>file+ & \verb+>file 2>&1+ \\
\verb+>&file+ & preferred form of \verb+&>file+ \\
\verb+&>>file+ & \verb+>>file 2>&1+ \\
\verb+[m]<&int+ & make fd \texttt{m} a copy of input fd \texttt{int}. \\
\verb+[m]<&-+ & close fd \texttt{m}. \\
\verb+[n]>&int+ & make fd \texttt{n} a copy of output fd \texttt{int}. \\
\verb+[n]>&-+ & close fd \texttt{n}. \\
\verb+[m]<&int-+ & \verb+[m]<&int int<&-+ \\
\verb+[n]>&int-+ & \verb+[n]>&int int>&-+ \\
\verb+[m]<>file+ & open \texttt{file} for reading and writing \\
\verb+[m]<<<string+ & expansion (all but WF) of \texttt{string} on fd \texttt{m} \\
\end{tabular}
`value` undergoes all expansions except `W` (exception:
`$@`) and `Q`.
* \verb+[n]>file+ **erases** ("clobbers") `file` if it exists (and creates it
otherwise)
* `m` defaults to `0` (`stdin`), `n` to `1` (`stdout`).
* `file` and `int` undergo all expansions; `int` must expand to an int.
Positional Parameters
---------------------
Here Documents
--------------
`$1`, `$2`, \ldots{} contain the program's 1st, 2nd, etc. arguments (if any,
otherwise unset). Can be reset, but only with `set` and `shift`. Use braces for
arguments beyond 9^th^: `$10` $=$ `${1}0` $\neq$ `${10}`.
Read from code itself until a line consisting exactly of `delimiter`. This
becomes the input of fd `m` (defaults to `0`).
Some Special Parameters
------------------
\begin{verbatim}
[m]<<[-]end
code
delimiter
\end{verbatim}
All are read-only.
* `end` undergoes no expansion
* `delimiter` is `end` after quote removal
* `end` unquoted: `code` is expanded (VCA - use `\` to escape, `\newline` ignored)
* `end` with quotes (`'` or `"`, anywhere): `code` is not expanded.
\begin{tabular}{p{1.0cm}p{7cm}}
\textbf{param} & \textbf{meaning} \\
\texttt{\$0} & pathname of script \\
\texttt{\$*}, \texttt{\$@} & \texttt{\$1 \$2 ... \$n} \\
\texttt{"\$*"} & \texttt{"\$1s\$2s...\$n"}; separator \texttt{s} is the 1\textsuperscript{st} char of \texttt{\$IFS} (\texttt{space} if unset, empty if null) \\
\texttt{"\$@"} & \texttt{"\$1" "\$2" ... "\$n"} \\
\texttt{"A\$@B"} & \texttt{"A\$1" "\$2" ... "\$nB"} \\
\texttt{\$\#} & number of positional parameters \\
\texttt{\$?} & exit status of last foreground pipeline\\
\texttt{\$!} & PID of last asynchronous command \\
Special Characters
==================
The following characters can have special meaning.
\begin{tabular}{p{3.0cm}p{5cm}}
\textbf{characters} & \textbf{function/category} \\
\verb+space tab newline+ & whitespace metacharacters \\
\verb+| & ( ) < > ;+ & other metacharacters \\
\verb+* ? [ ]+ & glob/test \\ %*
\verb+" ' \+ & quoting \\
\verb+$ `+ & expansion \\
\verb+#+ & comment \\
\verb+=+ & assignment \\
\verb+!+ & logical NOT \\
\end{tabular}
When WF are not done, `$*` and `$@` behave like `"$*"` (separator: 1^st^ char
of `$IFS` or `space`, respectively).
Some characters can be special in several contexts.
Special characters regain their normal ("literal") status if _quoted_. **Rule
of thumb**: a character is special (in a given context) iff quoting it makes a
difference.
Quoting
=======
Makes special characters literal.
* `\` ("escape") makes the next character literal, except `newline`
* all characters between `'` are literal; no `'` can occur between `'`
* all characters between `"` are literal, except `$` \verb$`$ `\`
* ANSI-C: `\n`, `\t` within `$'string'` $\rightarrow$ `newline`, `tab`, etc.
----
Compound Commands
=================
In the following, `;` can be replaced with `newline`.
Loops
=====
-----
`newlines` can be replaced with `;`
### _for_ loop - variant 1
```bash
for name [ in [ word ... ] ; ]
do
list ;
done
# arithmetic evaluation in ((...))
for (( expr1 ; expr2 ; expr3 ))
do
list ;
done
while list1; do list2; done
until list1; do list2; done
```
`for` _`name`_ `in` _`words`..._; `do` _`list`_; `done`
_`words...`_ are expanded (all expansions); _`list`_ is executed once for each
item in the resulting list, binding _`name`_ to each in turn. If `in words...`
is omitted, it defaults to `in "$@"`.
### _for_ loop - variant 2
`for ((`_`expr1`_`;`_`expr2`_`;`_`expr3`_`)); do` _`list`_`; done`
_`expr1`_ is evaluated once; then _`expr2`_ is evaluated repeatedly until it is
0; as long as it is nonzero _`list`_ then _`expr3`_ are evaluated.
_`expr[123]`_ are evaluated using _shell arithmetic_.
### _while_ loop
`while` _`list1`_`;` `do` _`list2`_`;` `done`
Execute _`list2`_ while the exit status of _`list1`_ is zero.
`until` _`list1`_`;` `do` _`list2`_`;` `done`
Execute _`list2`_ while the exit status of _`list1`_ is nonzero.
Loop control: `break [n]`, `continue [n]`
Conditionals
============
------------
`if - then - else`
----
### `if - then - else`
```bash
if cmd1; then
......@@ -252,8 +302,7 @@ are lists.
There are $\geq 0$ `elif` clauses, and $\leq 1$ `else` clause.
`case - in`
------
### `case - in`
```bash
case word in
......@@ -316,6 +365,46 @@ Conditional Expressions
\end{tabbing}
Parameters
==========
Assignment
----------
```bash
var=value # no spaces!
```
`value` undergoes all expansions except `W` (exception:
`$@`) and `Q`.
Positional Parameters
---------------------
`$1`, `$2`, \ldots{} contain the program's 1st, 2nd, etc. arguments (if any,
otherwise unset). Can be reset, but only with `set` and `shift`. Use braces for
arguments beyond 9^th^: `$10` $=$ `${1}0` $\neq$ `${10}`.
Some Special Parameters
------------------
All are read-only.
\begin{tabular}{p{1.0cm}p{7cm}}
\textbf{param} & \textbf{meaning} \\
\texttt{\$0} & pathname of script \\
\texttt{\$*}, \texttt{\$@} & \texttt{\$1 \$2 ... \$n} \\
\texttt{"\$*"} & \texttt{"\$1s\$2s...\$n"}; separator \texttt{s} is the 1\textsuperscript{st} char of \texttt{\$IFS} (\texttt{space} if unset, empty if null) \\
\texttt{"\$@"} & \texttt{"\$1" "\$2" ... "\$n"} \\
\texttt{"A\$@B"} & \texttt{"A\$1" "\$2" ... "\$nB"} \\
\texttt{\$\#} & number of positional parameters \\
\texttt{\$?} & exit status of last foreground pipeline\\
\texttt{\$!} & PID of last asynchronous command \\
\end{tabular}
When WF are not done, `$*` and `$@` behave like `"$*"` (separator: 1^st^ char
of `$IFS` or `space`, respectively).
Shell Arithmetic
================
......@@ -346,71 +435,6 @@ priority):
Augmented assignment (`+=`, `|=`, `<<=`, etc.) has the same priority as `=`.
Redirections
============
(Details: ```man bash, / ^REDIR```)
There may be more than one, and order matters.
\begin{tabular}{p{2.0cm}p{6cm}}
\textbf{operator} & \textbf{behaviour} \\
\verb+[m]<file+ & open \texttt{file} for reading on fd \texttt{m} \\ % >
\verb+[n]>[|]file+ & open \texttt{file} for writing on fd \texttt{n}; \texttt{>|} ignores \texttt{noclobber} \\
\verb+[n]>>file+ & open \texttt{file} for appending on fd \texttt{n} \\
\verb+&>file+ & \verb+>file 2>&1+ \\
\verb+>&file+ & preferred form of \verb+&>file+ \\
\verb+&>>file+ & \verb+>>file 2>&1+ \\
\verb+[m]<&int+ & make fd \texttt{m} a copy of input fd \texttt{int}. \\
\verb+[m]<&-+ & close fd \texttt{m}. \\
\verb+[n]>&int+ & make fd \texttt{n} a copy of output fd \texttt{int}. \\
\verb+[n]>&-+ & close fd \texttt{n}. \\
\verb+[m]<&int-+ & \verb+[m]<&int int<&-+ \\
\verb+[n]>&int-+ & \verb+[n]>&int int>&-+ \\
\verb+[m]<>file+ & open \texttt{file} for reading and writing \\
\verb+[m]<<<string+ & expansion (all but WF) of \texttt{string} on fd \texttt{m} \\
\end{tabular}
* \verb+[n]>file+ **erases** ("clobbers") `file` if it exists (and creates it
otherwise)
* `m` defaults to `0` (`stdin`), `n` to `1` (`stdout`).
* `file` and `int` undergo all expansions; `int` must expand to an int.
Here Documents
--------------
Read from code itself until a line consisting exactly of `delimiter`. This
becomes the input of fd `m` (defaults to `0`).
\begin{verbatim}
[m]<<[-]end
code
delimiter
\end{verbatim}
* `end` undergoes no expansion
* `delimiter` is `end` after quote removal
* `end` unquoted: `code` is expanded (VCA - use `\` to escape, `\newline` ignored)
* `end` with quotes (`'` or `"`, anywhere): `code` is not expanded.
Special Characters
==================
Characters which can have special meaning.
* [Metacharacters](#tokenizing)
* `$`, \verb+`+ (between `"`)
* `\` (between `"` and before `$` \verb+`+ `"` `\` or `newline`)
Quoting
=======
Makes special characters literal.
* `\` makes the next character literal, except `newline`
* all characters between `'` are literal; no `'` can occur between `'`
* all characters between `"` are literal, except `$` \verb$`$ `\`
* ANSI-C: `\n`, `\t` within `$'string'` $\rightarrow$ `newline`, `tab`, etc.
Pattern Matching ("Globbing")
=============================
......@@ -503,3 +527,4 @@ TODO
* idioms: read x y < <(...) (use process substitution to set variables)
* f -o a1 a2 -- you can pass switches and options to functions
* regexps `[[ $var =~ regexp ]]`
* fail options
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment