summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cli.md88
1 files changed, 59 insertions, 29 deletions
diff --git a/cli.md b/cli.md
index 07a3b73..ee3843b 100644
--- a/cli.md
+++ b/cli.md
@@ -1,5 +1,11 @@
1% The Linux command-line 1% The Linux command-line
2 2
3---
4subtitle: WTF is Linux
5---
6
7[back](index.md)
8
3 9
4## Purpose 10## Purpose
5 11
@@ -11,15 +17,17 @@ a separate window, buttons, etc., what I mean here is a program that only
11outputs text for us to see. 17outputs text for us to see.
12 18
13While the command-line was mostly created because historically, computers 19While the command-line was mostly created because historically, computers
14weren't quite powerful[^teletypewriter], it is still used to this day by 20weren't quite powerful[^teletypewriter], it's still used to this day by a large
15a large amount of people for various reasons (some people find it handy, 21amount of people for various reasons (some people find it handy, clearer,
16clearer, faster, or even more elegant). 22faster, or even more elegant).
17 23
18[^teletypewriter]: 24[^teletypewriter]:
19 Another possible reason it was created is because computers didn't even 25 It's also likely that it was created is because computers didn't even have
20 have screen before, but instead printed text on paper 26 screens before, but instead [printed text on paper](https://en.wikipedia.org/wiki/Teleprinter).
21 27
22TODO: link teletypewriter video by Drew Devault 28 [Example](https://spacepub.space/videos/watch/d8943b2d-8280-497b-85ec-bc282ec2afdc).
29
30 [Other example with older machine](https://www.youtube.com/watch?v=2XLZ4Z8LpEE).
23 31
24 32
25## Usage 33## Usage
@@ -28,11 +36,11 @@ When you open a terminal, you will be greeted by a prompt that will kindly
28"ask" you to input a command. 36"ask" you to input a command.
29 37
30A command is composed of a command name (or verb), and 0, 1, or more arguments. 38A command is composed of a command name (or verb), and 0, 1, or more arguments.
31The verb and each arguments are separated by 1 or more spaces. 39The verb and each argument are separated by 1 or more spaces.
32 40
33Just like in English grammar, the arguments have different meanings for 41Just like in English grammar, the arguments have different meanings for
34different verbs. For example, when you use the verb `cp` (for "copy"), the 42different verbs. For example, when you use the verb `cp` (for "copy"), the
35first arguments are the "source" (i.e. the files to copy), and the last 43first arguments are the "source" (that is, the files to copy), and the last
36argument is the destination: 44argument is the destination:
37 45
38```bash 46```bash
@@ -42,7 +50,7 @@ user@host:~$ cp file_1.txt file_2.txt
42# | | | 50# | | |
43# | '-------------'- arguments 51# | '-------------'- arguments
44# | 52# |
45# '---------------------- command name (verb) 53# '---------------------- command name (verb)
46``` 54```
47 55
48But when you use the verb `mkdir` (for "make directory"), all the arguments are 56But when you use the verb `mkdir` (for "make directory"), all the arguments are
@@ -76,8 +84,8 @@ user@host:~$ my_cp ~~file_1.txt//file_2.txt~~ _~overwrite
76 84
77But that would be pretty horrible. 85But that would be pretty horrible.
78 86
79More than the fact that this syntax is really ugly to look at, we need a common 87More than the fact that this syntax is hideous, we need a common syntax to
80syntax to specify options, flags, etc. 88specify options, flags, etc.
81 89
82And this is where the POSIX convention, and the GNU convention comes in. 90And this is where the POSIX convention, and the GNU convention comes in.
83 91
@@ -105,28 +113,32 @@ user@host:~$ ls --sort=size
105user@host:~$ ls --sort size 113user@host:~$ ls --sort size
106``` 114```
107 115
108TODO: we call options without argument a "flag" 116Options without an argument are commonly called "flags."
117
118Again, these are still conventions, developers are free to ignore those. To
119this day, you can still find programs that don't respect either the POSIX nor
120the GNU conventions.
109 121
110TODO: Remind that these are still convention, some programs don't respect that 122TODO: link to example with https://explainshell.com
111 123
112## Components 124## Components
113 125
114While using the term "command-line" might suggest that it is composed of 126While using the term "command-line" might suggest that it's composed of
115a single component, it is not. Should you have a problem to debug, I always 127a single component, it isn't. Should you have a problem to debug, I always find
116find it useful to know how the different pieces interact with on another. 128it useful to know how the different pieces interact with on another.
117 129
118So, here are the different pieces that are the most useful to us[^tty]: 130So, here are the different pieces that are the most useful to us[^tty]:
119 131
120[^tty]: 132[^tty]:
121 Some components are omitted for simplicity's sake, like the concept of 133 Some components are omitted for simplicity's sake, like the concept of
122 "tty", "pty", and others 134 "tty," "pty," and others
123 135
124The terminal / terminal emulator 136The terminal / terminal emulator
125: Renders text on the screen 137: Renders text on the screen
126 138
127The prompt / REPL 139The prompt / REPL
128: A part of the shell that interactively asks for command-lines, and shows you 140: A part of the shell that interactively asks for command-lines, and shows you
129 some informations like what user you are logged as, what directory you're 141 some information like what user you are logged as, what directory you're
130 currently in, etc. 142 currently in, etc.
131 143
132The shell 144The shell
@@ -136,10 +148,10 @@ The shell
136### The terminal 148### The terminal
137 149
138As I said above, the terminal (or terminal emulator) is the program that 150As I said above, the terminal (or terminal emulator) is the program that
139renders the text on the screen. It is therefore *not* the kind of program I was 151renders the text on the screen. It's therefore *not* the kind of program I was
140talking about in the [purpose] section. 152talking about in the [purpose] section.
141 153
142It is a graphical program that creates a new window, runs a text-only program 154It's a graphical program that creates a new window, runs a text-only program
143as a child process, and for each character outputted by the text program, 155as a child process, and for each character outputted by the text program,
144convert the character into pixels that are rendered in the window for our eyes 156convert the character into pixels that are rendered in the window for our eyes
145to see. 157to see.
@@ -160,7 +172,7 @@ It would be a pain to start a terminal emulator for each and every little
160commands we wanted to execute[^close-on-exit]. 172commands we wanted to execute[^close-on-exit].
161 173
162[^close-on-exit]: 174[^close-on-exit]:
163 In fact it is so common to run a prompt in a terminal emulator that most 175 In fact it's so common to run a prompt in a terminal emulator that most
164 terminal just closes the window and exit when the text program exits. If 176 terminal just closes the window and exit when the text program exits. If
165 you were to run `mkdir new_dir` as the terminal command, you would see 177 you were to run `mkdir new_dir` as the terminal command, you would see
166 a new window being created, and it would close after ~0.002 seconds. 178 a new window being created, and it would close after ~0.002 seconds.
@@ -169,7 +181,7 @@ And this is why we run an interactive text program: it's just easier, faster,
169and way less annoying. 181and way less annoying.
170 182
171When you first start a terminal in a new installation of a Linux system, you 183When you first start a terminal in a new installation of a Linux system, you
172will mostly likely be greeted with a Bash prompt. Bash being the name of one of 184will mostly likely be greeted with a Bash prompt, Bash being the name of one of
173the several shells that exists in this world. 185the several shells that exists in this world.
174 186
175Your prompt will likely resemble this (annotations added): 187Your prompt will likely resemble this (annotations added):
@@ -178,7 +190,7 @@ Your prompt will likely resemble this (annotations added):
178```bash 190```bash
179# ,------ username you are logged in as 191# ,------ username you are logged in as
180# | 192# |
181# | ,---- directory you are in 193# | ,---- directory you are in,
182# | | '~' means your user directory 194# | | '~' means your user directory
183# vvvv v 195# vvvv v
184 user@host:~$ 196 user@host:~$
@@ -191,30 +203,40 @@ Your prompt will likely resemble this (annotations added):
191 203
192``` 204```
193 205
194But just like everything is in the UNIX world, everything about it is 206But just like everything is in the UNIX world, it's very customizable:
195customizable:
196 207
197- You can for example run a prompt from a different shell: instead of using the 208- You can for example run a prompt from a different shell: instead of using the
198 Bash prompt, you can use the one from Zsh, Fish, Csh, etc. 209 Bash prompt, you can use the one from Zsh, Fish, Csh, Nushell, etc.
199- You can customize the prompt itself using the configuration system of Bash, 210- You can customize the prompt itself using the configuration system of Bash,
200 Zsh, etc. 211 Zsh, etc.
201 212
202TODO: link doc of bash prompt 213For example in Bash and many other shells, you can change your prompt by
214changing the `$PS1` variable. Try executing this in a Bash shell (you may need
215to launch `bash --norc` before):
216
217```bash
218PS1='\A \u@\h \w (\s) \$ '
219```
203 220
204 221
222Full documentation of the prompt's "special characters"
223[here](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Controlling-the-Prompt).
224
205### The shell 225### The shell
206 226
207It might seem like the prompt is everything there is about Bash, Zsh, etc., but 227It might seem like the prompt is everything there is about Bash, Zsh, etc., but
208these programs are actually much more powerful than that: a shell is 228these programs are actually much more powerful than that: a shell is
209a full-size programming language! 229a full-size programming language!
210 230
211Like any programming language it is very useful for automating tasks, and the 231Like any programming language it's very useful for automating tasks, and the
212fact that a shell script (a program written in a shell language) is included in 232fact that a shell script (a program written in a shell language) is included in
213every Linux system, and that it is very quick to write makes it very common in 233every Linux system, and that it's very quick to write makes it very common in
214embedded systems and system administration. 234embedded systems and system administration.
215 235
216So, let's go over the particularities of the shell programming language. 236So, let's go over the particularities of the shell programming language.
217 237
238TODO: move these sections to new "Shell Scripting" article
239
218#### Basic interpretation of a command 240#### Basic interpretation of a command
219 241
220```c 242```c
@@ -234,8 +256,16 @@ int main(int argc, char** argv) {
234 256
235#### Variables and interpolation 257#### Variables and interpolation
236 258
259TODO
260
237#### Control flow 261#### Control flow
238 262
263TODO
264
239#### The various shell implementations 265#### The various shell implementations
240 266
267TODO
268
241## Further reading 269## Further reading
270
271TODO