Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| linux:bash [2022/02/10 09:44] – [Bash Scripting] mh | linux:bash [2026/01/11 11:57] (current) – [Control Operators] mh | ||
|---|---|---|---|
| Line 15: | Line 15: | ||
| Bash has **synchronous command execution** meaning it will execute commands one at a time and users cannot interact with bash while it is executing a command. | Bash has **synchronous command execution** meaning it will execute commands one at a time and users cannot interact with bash while it is executing a command. | ||
| - | Bash is **not a strict interpreter**. The discipline of writing clear, safe and precise code lies heavily upon the user. | ||
| <WRAP round important> | <WRAP round important> | ||
| - | **Don' | + | Bash is **not a strict interpreter**. |
| + | |||
| + | **Beware of the ambiguity** we are used to while expressing ourselves with human languages. | ||
| + | |||
| + | The **discipline of writing clear, safe and precise code** lies heavily upon the user. | ||
| + | |||
| + | __**Don' | ||
| </ | </ | ||
| Line 30: | Line 35: | ||
| Many command types are syntax sugar: their effect can be achieved differently, | Many command types are syntax sugar: their effect can be achieved differently, | ||
| </ | </ | ||
| + | |||
| + | ==== Variables ==== | ||
| + | |||
| + | Before a command' | ||
| ==== Basic commands ==== | ==== Basic commands ==== | ||
| Line 36: | Line 45: | ||
| Bash performs a search using this name and looks for : | Bash performs a search using this name and looks for : | ||
| - | *alias (first) | + | *alias (before anything else) |
| *functions | *functions | ||
| *built-in commands | *built-in commands | ||
| Line 54: | Line 63: | ||
| $ echo $name | $ echo $name | ||
| john | john | ||
| + | </ | ||
| + | ---- | ||
| + | |||
| + | === type === | ||
| + | |||
| + | Displays information on a command, and where it is stored. | ||
| + | |||
| + | //Note that the **type** command and the **which** program are different and give different outputs// | ||
| + | |||
| + | **Example** | ||
| + | <code bash> | ||
| + | $ type ls | ||
| + | ls is aliased to 'ls -al' | ||
| </ | </ | ||
| ---- | ---- | ||
| Line 91: | Line 113: | ||
| ---- | ---- | ||
| + | === 2>/ | ||
| + | |||
| + | <code bash> | ||
| + | $ something 2>/ | ||
| + | </ | ||
| + | |||
| + | This means: | ||
| + | |||
| + | Try something, but if it errors (exit code ≠ 0): | ||
| + | * silence the error message (send to ''/ | ||
| + | * immediately run '' | ||
| + | * Overall '' | ||
| + | |||
| + | In plain English: | ||
| + | |||
| + | “Try to do the cleanup. | ||
| + | If there’s nothing to clean, that’s fine. Move on.” | ||
| + | |||
| + | This pattern is **not appropriate** when: | ||
| + | * Failure is meaningful | ||
| + | * You want to be alerted | ||
| + | * Data loss is possible | ||
| + | * You are debugging | ||
| + | |||
| + | In those cases, we want '' | ||
| + | |||
| + | So this is not about hiding problems. | ||
| + | It’s about acknowledging expected non-problems. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | === && AND Operator === | ||
| + | |||
| + | This control operator tells bash to run a second command **only if the first command before it succeeded**. | ||
| + | |||
| + | If the first command fails, the second command is entirely skipped. | ||
| + | |||
| + | **Example** | ||
| + | <code bash> | ||
| + | $ rm hello.txt && echo " | ||
| + | rm: cannot remove ' | ||
| + | </ | ||
| + | |||
| + | Because the file did not exist, the '' | ||
| ===== Arguments ===== | ===== Arguments ===== | ||
| Line 97: | Line 163: | ||
| They can be a filename, a variable name, the name of a program or just a litteral. | They can be a filename, a variable name, the name of a program or just a litteral. | ||
| + | |||
| + | Multiple arguments can be used, separated by a blank space | ||
| </ | </ | ||
| Line 179: | Line 247: | ||
| There can be **no whitespace** around the = operator ! | There can be **no whitespace** around the = operator ! | ||
| - | If a whitespace is required in the variable, use '' | + | If a whitespace is required in the variable, use 'single |
| </ | </ | ||
| Line 319: | Line 387: | ||
| As a result, both file descriptors are connected to the same stream. | As a result, both file descriptors are connected to the same stream. | ||
| + | Be careful not to write something like '' | ||
| </ | </ | ||
| Line 355: | Line 424: | ||
| </ | </ | ||
| - | This rather more precise alternative invokes the ' | + | This rather more precise alternative invokes the ' |
| <code bash> | <code bash> | ||
| # | # | ||