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/11 11:02] – [Generalities] mh | linux:bash [2026/10/31 22:42] (current) – [claude] fix command expansion, tilde and echo examples, heading levels, https link, typos claude | ||
|---|---|---|---|
| Line 11: | Line 11: | ||
| Bash can be used **interactively** in the terminal emulator or **non-interactively** as a written script of commands that is executed. | Bash can be used **interactively** in the terminal emulator or **non-interactively** as a written script of commands that is executed. | ||
| - | When bash starts a program, the system creates a running **process** for it. Bash creates **file descriptors** | + | When bash starts a program, the system creates a running **process** for it. Bash creates **file descriptors** |
| 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. | ||
| Line 19: | Line 19: | ||
| Bash is **not a strict interpreter**. | Bash is **not a strict interpreter**. | ||
| - | Beware of the ambiguity we are used to while expressing ourselves with human languages. | + | **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. | + | The **discipline of writing clear, safe and precise code** lies heavily upon the user. |
| - | **Don' | + | __**Don' |
| </ | </ | ||
| Line 35: | 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 41: | 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 59: | 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 95: | Line 112: | ||
| </ | </ | ||
| ---- | ---- | ||
| + | |||
| + | === 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 101: | Line 163: | ||
| **Arguments are a sequence of characters considered as a single unit by the shell.** | **Arguments are a sequence of characters considered as a single unit by the shell.** | ||
| - | 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 literal. |
| + | |||
| + | Multiple arguments can be used, separated by a blank space | ||
| </ | </ | ||
| Line 110: | Line 174: | ||
| Escaping is the act of adding a single \ before the character we want to escape. | Escaping is the act of adding a single \ before the character we want to escape. | ||
| - | Quoting is a better practice than escaping and should be use more often than not. Quoting is sometimes unnecessary but it is rarely wrong to quote. | + | Quoting is a better practice than escaping and should be used more often than not. Quoting is sometimes unnecessary but it is rarely wrong to quote. |
| <WRAP round important> | <WRAP round important> | ||
| Line 129: | Line 193: | ||
| $ read -p 'Which user would you like to remove from your system? ' username | $ read -p 'Which user would you like to remove from your system? ' username | ||
| Which user would you like to remove from your system? | Which user would you like to remove from your system? | ||
| - | #Note that the user accidentaly inputed | + | #Note that the user accidentally entered |
| $ rm -vr / | $ rm -vr / | ||
| Line 184: | Line 248: | ||
| 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 190: | Line 254: | ||
| <code bash> | <code bash> | ||
| - | $ contents=" | + | $ contents=" |
| # Don't forget to " | # Don't forget to " | ||
| </ | </ | ||
| Line 202: | Line 266: | ||
| <code bash> | <code bash> | ||
| $ name=mh time=8 | $ name=mh time=8 | ||
| - | $ echo " | + | $ echo " |
| mh, it is 8h. | mh, it is 8h. | ||
| </ | </ | ||
| Line 212: | Line 276: | ||
| === WHITESPACE === | === WHITESPACE === | ||
| - | Whitespace in bash have a special meaning : they split commands into arguments. | + | Whitespace in bash has a special meaning : they split commands into arguments. |
| === * === | === * === | ||
| - | A star or asterix | + | A star or asterisk |
| ---- | ---- | ||
| Line 248: | Line 312: | ||
| ==== Tilde expansion ==== | ==== Tilde expansion ==== | ||
| - | **Tilde expansion** happens earlier in the parser | + | **Tilde expansion** happens earlier in the parser |
| It is always replaced by an explicit pathname pointing to the home directory of the user or of another user explicitly named after the tilde. | It is always replaced by an explicit pathname pointing to the home directory of the user or of another user explicitly named after the tilde. | ||
| Line 257: | Line 321: | ||
| $ echo ~root | $ echo ~root | ||
| - | /var/root | + | /root |
| </ | </ | ||
| - | ===== Value Expansions ==== | + | ===== Value Expansions |
| **Command expansions** are a very useful value expansion example. You can call a command within a command by using $(...) syntax. | **Command expansions** are a very useful value expansion example. You can call a command within a command by using $(...) syntax. | ||
| Line 275: | Line 339: | ||
| You must **always** use " | You must **always** use " | ||
| </ | </ | ||
| + | |||
| ===== Redirection ===== | ===== Redirection ===== | ||
| Line 324: | Line 389: | ||
| 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 336: | Line 402: | ||
| <code bash> | <code bash> | ||
| #This convenience sugar syntax exists for directing both FD1 & FD2 to the same location | #This convenience sugar syntax exists for directing both FD1 & FD2 to the same location | ||
| - | $ping 127.0.0.1 &> | + | $ ping 127.0.0.1 &> |
| </ | </ | ||
| Line 343: | Line 409: | ||
| ==== Append to a file ==== | ==== Append to a file ==== | ||
| - | Use double | + | Use double |
| <code bash> | <code bash> | ||
| echo Hello > | echo Hello > | ||
| Line 349: | Line 415: | ||
| </ | </ | ||
| ---- | ---- | ||
| + | |||
| ===== Bash Scripting ===== | ===== Bash Scripting ===== | ||
| - | === Hashbangs === | + | ==== Hashbangs |
| To make a script out of a file containing bash commands, add a hashbang at the beginning of the file : | To make a script out of a file containing bash commands, add a hashbang at the beginning of the file : | ||
| Line 377: | Line 444: | ||
| Command names with a slash are always considered direct pathnames to the program to execute. | Command names with a slash are always considered direct pathnames to the program to execute. | ||
| </ | </ | ||
| - | ===== Ressources | + | |
| + | ===== Resources | ||
| *[[https:// | *[[https:// | ||
| *[[https:// | *[[https:// | ||
| *[[https:// | *[[https:// | ||
| - | *[[http:// | + | *[[https:// |