diff options
author | Minijackson <minijackson@riseup.net> | 2020-09-21 17:41:26 +0200 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2020-09-21 17:41:26 +0200 |
commit | b2151643778c250a60addb48d8b9deed559575cd (patch) | |
tree | fd067caeaae9037771b305ac8c8cb75b86386b35 /cli.md | |
parent | 6f1f4e89c2e30445b01cfbac95fc6abb2cae3980 (diff) | |
download | wtf-is-linux-website-b2151643778c250a60addb48d8b9deed559575cd.tar.gz wtf-is-linux-website-b2151643778c250a60addb48d8b9deed559575cd.zip |
cli: expand on prompt + small fixes
Diffstat (limited to 'cli.md')
-rw-r--r-- | cli.md | 88 |
1 files changed, 59 insertions, 29 deletions
@@ -1,5 +1,11 @@ | |||
1 | % The Linux command-line | 1 | % The Linux command-line |
2 | 2 | ||
3 | --- | ||
4 | subtitle: 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 | |||
11 | outputs text for us to see. | 17 | outputs text for us to see. |
12 | 18 | ||
13 | While the command-line was mostly created because historically, computers | 19 | While the command-line was mostly created because historically, computers |
14 | weren't quite powerful[^teletypewriter], it is still used to this day by | 20 | weren't quite powerful[^teletypewriter], it's still used to this day by a large |
15 | a large amount of people for various reasons (some people find it handy, | 21 | amount of people for various reasons (some people find it handy, clearer, |
16 | clearer, faster, or even more elegant). | 22 | faster, 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 | ||
22 | TODO: 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 | ||
30 | A command is composed of a command name (or verb), and 0, 1, or more arguments. | 38 | A command is composed of a command name (or verb), and 0, 1, or more arguments. |
31 | The verb and each arguments are separated by 1 or more spaces. | 39 | The verb and each argument are separated by 1 or more spaces. |
32 | 40 | ||
33 | Just like in English grammar, the arguments have different meanings for | 41 | Just like in English grammar, the arguments have different meanings for |
34 | different verbs. For example, when you use the verb `cp` (for "copy"), the | 42 | different verbs. For example, when you use the verb `cp` (for "copy"), the |
35 | first arguments are the "source" (i.e. the files to copy), and the last | 43 | first arguments are the "source" (that is, the files to copy), and the last |
36 | argument is the destination: | 44 | argument 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 | ||
48 | But when you use the verb `mkdir` (for "make directory"), all the arguments are | 56 | But 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 | ||
77 | But that would be pretty horrible. | 85 | But that would be pretty horrible. |
78 | 86 | ||
79 | More than the fact that this syntax is really ugly to look at, we need a common | 87 | More than the fact that this syntax is hideous, we need a common syntax to |
80 | syntax to specify options, flags, etc. | 88 | specify options, flags, etc. |
81 | 89 | ||
82 | And this is where the POSIX convention, and the GNU convention comes in. | 90 | And this is where the POSIX convention, and the GNU convention comes in. |
83 | 91 | ||
@@ -105,28 +113,32 @@ user@host:~$ ls --sort=size | |||
105 | user@host:~$ ls --sort size | 113 | user@host:~$ ls --sort size |
106 | ``` | 114 | ``` |
107 | 115 | ||
108 | TODO: we call options without argument a "flag" | 116 | Options without an argument are commonly called "flags." |
117 | |||
118 | Again, these are still conventions, developers are free to ignore those. To | ||
119 | this day, you can still find programs that don't respect either the POSIX nor | ||
120 | the GNU conventions. | ||
109 | 121 | ||
110 | TODO: Remind that these are still convention, some programs don't respect that | 122 | TODO: link to example with https://explainshell.com |
111 | 123 | ||
112 | ## Components | 124 | ## Components |
113 | 125 | ||
114 | While using the term "command-line" might suggest that it is composed of | 126 | While using the term "command-line" might suggest that it's composed of |
115 | a single component, it is not. Should you have a problem to debug, I always | 127 | a single component, it isn't. Should you have a problem to debug, I always find |
116 | find it useful to know how the different pieces interact with on another. | 128 | it useful to know how the different pieces interact with on another. |
117 | 129 | ||
118 | So, here are the different pieces that are the most useful to us[^tty]: | 130 | So, 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 | ||
124 | The terminal / terminal emulator | 136 | The terminal / terminal emulator |
125 | : Renders text on the screen | 137 | : Renders text on the screen |
126 | 138 | ||
127 | The prompt / REPL | 139 | The 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 | ||
132 | The shell | 144 | The shell |
@@ -136,10 +148,10 @@ The shell | |||
136 | ### The terminal | 148 | ### The terminal |
137 | 149 | ||
138 | As I said above, the terminal (or terminal emulator) is the program that | 150 | As I said above, the terminal (or terminal emulator) is the program that |
139 | renders the text on the screen. It is therefore *not* the kind of program I was | 151 | renders the text on the screen. It's therefore *not* the kind of program I was |
140 | talking about in the [purpose] section. | 152 | talking about in the [purpose] section. |
141 | 153 | ||
142 | It is a graphical program that creates a new window, runs a text-only program | 154 | It's a graphical program that creates a new window, runs a text-only program |
143 | as a child process, and for each character outputted by the text program, | 155 | as a child process, and for each character outputted by the text program, |
144 | convert the character into pixels that are rendered in the window for our eyes | 156 | convert the character into pixels that are rendered in the window for our eyes |
145 | to see. | 157 | to see. |
@@ -160,7 +172,7 @@ It would be a pain to start a terminal emulator for each and every little | |||
160 | commands we wanted to execute[^close-on-exit]. | 172 | commands 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, | |||
169 | and way less annoying. | 181 | and way less annoying. |
170 | 182 | ||
171 | When you first start a terminal in a new installation of a Linux system, you | 183 | When you first start a terminal in a new installation of a Linux system, you |
172 | will mostly likely be greeted with a Bash prompt. Bash being the name of one of | 184 | will mostly likely be greeted with a Bash prompt, Bash being the name of one of |
173 | the several shells that exists in this world. | 185 | the several shells that exists in this world. |
174 | 186 | ||
175 | Your prompt will likely resemble this (annotations added): | 187 | Your 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 | ||
194 | But just like everything is in the UNIX world, everything about it is | 206 | But just like everything is in the UNIX world, it's very customizable: |
195 | customizable: | ||
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 | ||
202 | TODO: link doc of bash prompt | 213 | For example in Bash and many other shells, you can change your prompt by |
214 | changing the `$PS1` variable. Try executing this in a Bash shell (you may need | ||
215 | to launch `bash --norc` before): | ||
216 | |||
217 | ```bash | ||
218 | PS1='\A \u@\h \w (\s) \$ ' | ||
219 | ``` | ||
203 | 220 | ||
204 | 221 | ||
222 | Full 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 | ||
207 | It might seem like the prompt is everything there is about Bash, Zsh, etc., but | 227 | It might seem like the prompt is everything there is about Bash, Zsh, etc., but |
208 | these programs are actually much more powerful than that: a shell is | 228 | these programs are actually much more powerful than that: a shell is |
209 | a full-size programming language! | 229 | a full-size programming language! |
210 | 230 | ||
211 | Like any programming language it is very useful for automating tasks, and the | 231 | Like any programming language it's very useful for automating tasks, and the |
212 | fact that a shell script (a program written in a shell language) is included in | 232 | fact that a shell script (a program written in a shell language) is included in |
213 | every Linux system, and that it is very quick to write makes it very common in | 233 | every Linux system, and that it's very quick to write makes it very common in |
214 | embedded systems and system administration. | 234 | embedded systems and system administration. |
215 | 235 | ||
216 | So, let's go over the particularities of the shell programming language. | 236 | So, let's go over the particularities of the shell programming language. |
217 | 237 | ||
238 | TODO: 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 | ||
259 | TODO | ||
260 | |||
237 | #### Control flow | 261 | #### Control flow |
238 | 262 | ||
263 | TODO | ||
264 | |||
239 | #### The various shell implementations | 265 | #### The various shell implementations |
240 | 266 | ||
267 | TODO | ||
268 | |||
241 | ## Further reading | 269 | ## Further reading |
270 | |||
271 | TODO | ||