From b2151643778c250a60addb48d8b9deed559575cd Mon Sep 17 00:00:00 2001 From: Minijackson Date: Mon, 21 Sep 2020 17:41:26 +0200 Subject: cli: expand on prompt + small fixes --- cli.md | 88 ++++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file 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 @@ % The Linux command-line +--- +subtitle: WTF is Linux +--- + +[back](index.md) + ## Purpose @@ -11,15 +17,17 @@ a separate window, buttons, etc., what I mean here is a program that only outputs text for us to see. While the command-line was mostly created because historically, computers -weren't quite powerful[^teletypewriter], it is still used to this day by -a large amount of people for various reasons (some people find it handy, -clearer, faster, or even more elegant). +weren't quite powerful[^teletypewriter], it's still used to this day by a large +amount of people for various reasons (some people find it handy, clearer, +faster, or even more elegant). [^teletypewriter]: - Another possible reason it was created is because computers didn't even - have screen before, but instead printed text on paper + It's also likely that it was created is because computers didn't even have + screens before, but instead [printed text on paper](https://en.wikipedia.org/wiki/Teleprinter). -TODO: link teletypewriter video by Drew Devault + [Example](https://spacepub.space/videos/watch/d8943b2d-8280-497b-85ec-bc282ec2afdc). + + [Other example with older machine](https://www.youtube.com/watch?v=2XLZ4Z8LpEE). ## Usage @@ -28,11 +36,11 @@ When you open a terminal, you will be greeted by a prompt that will kindly "ask" you to input a command. A command is composed of a command name (or verb), and 0, 1, or more arguments. -The verb and each arguments are separated by 1 or more spaces. +The verb and each argument are separated by 1 or more spaces. Just like in English grammar, the arguments have different meanings for different verbs. For example, when you use the verb `cp` (for "copy"), the -first arguments are the "source" (i.e. the files to copy), and the last +first arguments are the "source" (that is, the files to copy), and the last argument is the destination: ```bash @@ -42,7 +50,7 @@ user@host:~$ cp file_1.txt file_2.txt # | | | # | '-------------'- arguments # | -# '---------------------- command name (verb) +# '---------------------- command name (verb) ``` 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 But that would be pretty horrible. -More than the fact that this syntax is really ugly to look at, we need a common -syntax to specify options, flags, etc. +More than the fact that this syntax is hideous, we need a common syntax to +specify options, flags, etc. And this is where the POSIX convention, and the GNU convention comes in. @@ -105,28 +113,32 @@ user@host:~$ ls --sort=size user@host:~$ ls --sort size ``` -TODO: we call options without argument a "flag" +Options without an argument are commonly called "flags." + +Again, these are still conventions, developers are free to ignore those. To +this day, you can still find programs that don't respect either the POSIX nor +the GNU conventions. -TODO: Remind that these are still convention, some programs don't respect that +TODO: link to example with https://explainshell.com ## Components -While using the term "command-line" might suggest that it is composed of -a single component, it is not. Should you have a problem to debug, I always -find it useful to know how the different pieces interact with on another. +While using the term "command-line" might suggest that it's composed of +a single component, it isn't. Should you have a problem to debug, I always find +it useful to know how the different pieces interact with on another. So, here are the different pieces that are the most useful to us[^tty]: [^tty]: Some components are omitted for simplicity's sake, like the concept of - "tty", "pty", and others + "tty," "pty," and others The terminal / terminal emulator : Renders text on the screen The prompt / REPL : A part of the shell that interactively asks for command-lines, and shows you - some informations like what user you are logged as, what directory you're + some information like what user you are logged as, what directory you're currently in, etc. The shell @@ -136,10 +148,10 @@ The shell ### The terminal As I said above, the terminal (or terminal emulator) is the program that -renders the text on the screen. It is therefore *not* the kind of program I was +renders the text on the screen. It's therefore *not* the kind of program I was talking about in the [purpose] section. -It is a graphical program that creates a new window, runs a text-only program +It's a graphical program that creates a new window, runs a text-only program as a child process, and for each character outputted by the text program, convert the character into pixels that are rendered in the window for our eyes to see. @@ -160,7 +172,7 @@ It would be a pain to start a terminal emulator for each and every little commands we wanted to execute[^close-on-exit]. [^close-on-exit]: - In fact it is so common to run a prompt in a terminal emulator that most + In fact it's so common to run a prompt in a terminal emulator that most terminal just closes the window and exit when the text program exits. If you were to run `mkdir new_dir` as the terminal command, you would see 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, and way less annoying. When you first start a terminal in a new installation of a Linux system, you -will mostly likely be greeted with a Bash prompt. Bash being the name of one of +will mostly likely be greeted with a Bash prompt, Bash being the name of one of the several shells that exists in this world. Your prompt will likely resemble this (annotations added): @@ -178,7 +190,7 @@ Your prompt will likely resemble this (annotations added): ```bash # ,------ username you are logged in as # | -# | ,---- directory you are in +# | ,---- directory you are in, # | | '~' means your user directory # vvvv v user@host:~$ @@ -191,30 +203,40 @@ Your prompt will likely resemble this (annotations added): ``` -But just like everything is in the UNIX world, everything about it is -customizable: +But just like everything is in the UNIX world, it's very customizable: - You can for example run a prompt from a different shell: instead of using the - Bash prompt, you can use the one from Zsh, Fish, Csh, etc. + Bash prompt, you can use the one from Zsh, Fish, Csh, Nushell, etc. - You can customize the prompt itself using the configuration system of Bash, Zsh, etc. -TODO: link doc of bash prompt +For example in Bash and many other shells, you can change your prompt by +changing the `$PS1` variable. Try executing this in a Bash shell (you may need +to launch `bash --norc` before): + +```bash +PS1='\A \u@\h \w (\s) \$ ' +``` +Full documentation of the prompt's "special characters" +[here](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Controlling-the-Prompt). + ### The shell It might seem like the prompt is everything there is about Bash, Zsh, etc., but these programs are actually much more powerful than that: a shell is a full-size programming language! -Like any programming language it is very useful for automating tasks, and the +Like any programming language it's very useful for automating tasks, and the fact that a shell script (a program written in a shell language) is included in -every Linux system, and that it is very quick to write makes it very common in +every Linux system, and that it's very quick to write makes it very common in embedded systems and system administration. So, let's go over the particularities of the shell programming language. +TODO: move these sections to new "Shell Scripting" article + #### Basic interpretation of a command ```c @@ -234,8 +256,16 @@ int main(int argc, char** argv) { #### Variables and interpolation +TODO + #### Control flow +TODO + #### The various shell implementations +TODO + ## Further reading + +TODO -- cgit v1.2.3