Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
linux:bash [2021/04/03 19:24] – [Pathname expansion] mh | linux:bash [2022/02/28 11:51] – [Basic commands] mh | ||
---|---|---|---|
Line 3: | Line 3: | ||
===== Generalities ===== | ===== Generalities ===== | ||
- | Most of this comes from the excellent | + | Most of this comes from the greatest |
- | Bash is a **shell program** written to listen for **commands** from users and execute them. There are other shell programs (C shell, Z shell, Korn shell, etc.). | + | Bash is a **shell program** written to listen for **commands** from users and execute them. There are other shell programs (C shell, Z shell, Korn shell, etc.) so it is important to know for what shell you are writing code for. |
Bash uses **text-based interface** to interact with the users. It takes input (Std In) as text and displays output (Std Out) and error (Std Err) as text using a **terminal emulator** program. | Bash uses **text-based interface** to interact with the users. It takes input (Std In) as text and displays output (Std Out) and error (Std Err) as text using a **terminal emulator** program. | ||
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. | ||
- | **Don' | + | <WRAP round important> |
+ | 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' | ||
+ | </ | ||
===== Commands ===== | ===== Commands ===== | ||
<WRAP round info> | <WRAP round info> | ||
- | Bash commands tell bash to perform a certain unit of work. These units of work cannot be subdivided: bash needs to know the whole command to be able to execute it. | + | Bash commands tell bash to perform a certain unit of work. These units of work cannot be subdivided: bash needs to know the whole command to be able to execute it. Also, bash reads commands line-by-line. |
There are different kinds of commands for different types of operations. Some commands group other commands into blocks or test their result. | There are different kinds of commands for different types of operations. Some commands group other commands into blocks or test their result. | ||
Line 27: | 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 33: | 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 98: | Line 110: | ||
==== Quoting vs Escaping ==== | ==== Quoting vs Escaping ==== | ||
- | Quoting is the act of wrapping the argument using '' | + | Quoting is the act of wrapping the argument using ' ' and " ". |
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. | ||
Line 165: | Line 177: | ||
Shell variables are a way to store a value to be read and/or modified later. | Shell variables are a way to store a value to be read and/or modified later. | ||
- | Assignment is simple : | + | Assignment is simple : |
<code bash> | <code bash> | ||
$ name=mh | $ name=mh | ||
Line 185: | Line 198: | ||
</ | </ | ||
- | To re-use a variable we use parameter expansion, prefixing the name of our variable with a $ sign. Whenever $ is prefixed, something is probably being expanded (a parameter, the output of a command, or the result of an arithmetic operation) | + | To re-use a variable we use parameter expansion, prefixing the name of our variable with a \$ sign. Whenever |
RULE : Whenever you expand : you **must** " | RULE : Whenever you expand : you **must** " | ||
Line 201: | Line 214: | ||
==== Globs/ | ==== Globs/ | ||
+ | |||
+ | === WHITESPACE === | ||
+ | Whitespace in bash have a special meaning : they split commands into arguments. | ||
+ | |||
=== * === | === * === | ||
A star or asterix matches any kind of text, even no text at all. | A star or asterix matches any kind of text, even no text at all. | ||
Line 342: | Line 359: | ||
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 : | ||
- | This is a direct path to bash in many GNU/Linux distributions | + | This is a direct path to bash in many GNU/Linux distributions |
<code bash> | <code bash> | ||
# | # | ||
</ | </ | ||
- | This invokes the ' | + | This rather more precise alternative |
<code bash> | <code bash> | ||
# | # |