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 [2020/01/13 20:52] – [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 149: | Line 161: | ||
In other words, we use expansion to build generic commands that adapt to different situations by // | In other words, we use expansion to build generic commands that adapt to different situations by // | ||
</ | </ | ||
+ | |||
+ | |||
**Expansion is always performed by bash itself**, and always before actually running the command! | **Expansion is always performed by bash itself**, and always before actually running the command! | ||
Line 157: | Line 171: | ||
#It will be given a list of all files in the current directory | #It will be given a list of all files in the current directory | ||
</ | </ | ||
+ | |||
+ | ===Bash parameters=== | ||
+ | |||
+ | **Bash parameters** are regions in memory where you can temporarily store some information for later use. | ||
+ | |||
+ | Shell variables are a way to store a value to be read and/or modified later. | ||
+ | Assignment is simple : | ||
+ | |||
+ | <code bash> | ||
+ | $ name=mh | ||
+ | $ echo " | ||
+ | Hello, mh | ||
+ | </ | ||
+ | |||
+ | <WRAP round important> | ||
+ | There can be **no whitespace** around the = operator ! | ||
+ | |||
+ | If a whitespace is required in the variable, use '' | ||
+ | </ | ||
+ | |||
+ | A command expansion may be assigned to a variable : | ||
+ | |||
+ | <code bash> | ||
+ | $ contents=" | ||
+ | # Don't forget to " | ||
+ | </ | ||
+ | |||
+ | 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) | ||
+ | |||
+ | RULE : Whenever you expand : you **must** " | ||
+ | |||
+ | You can use {curly brackets} to tell bash where the variable starts and ends. This can be useful to prefix/ | ||
+ | |||
+ | <code bash> | ||
+ | $ name=mh time=8 | ||
+ | $ echo " | ||
+ | mh, it is 8h. | ||
+ | </ | ||
+ | |||
+ | We can use parameter expansion operators to modify the value that is expanded. This does not modify the value stored in the variable. See the guide for more details | ||
+ | |||
==== 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 205: | Line 264: | ||
</ | </ | ||
+ | ===== Value Expansions ==== | ||
+ | |||
+ | **Command expansions** are a very useful value expansion example. You can call a command within a command by using $(...) syntax. | ||
+ | |||
+ | <code bash> | ||
+ | $ echo 'Hello world.' | ||
+ | $ cat hello.txt | ||
+ | Hello world. | ||
+ | $ echo "The file < | ||
+ | The file < | ||
+ | </ | ||
+ | |||
+ | <WRAP round important> | ||
+ | You must **always** use " | ||
+ | </ | ||
===== Redirection ===== | ===== Redirection ===== | ||
Line 285: | 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> | ||
# | # | ||
Line 312: | Line 386: | ||
*[[https:// | *[[https:// | ||
*[[https:// | *[[https:// | ||
+ | *[[http:// |