Time to study: ~45 min
You will learn: echo, the difference between CLI and GUI, the terminal words everyone mixes up, how to make variables, the two kinds of quotes, and the Tab key that makes all of it faster.
← back to Terminal 101 | next: Chapter 2 →
1. the very basic command u can ever use
echo "Hello World"this is basically the same as writing “Hello World” in a text editor, it prints
Hello Worldto the terminal.
Hello World
2. why do i really need to learn the CLI
to get the idea of how to use the terminal we should first understand what is the CLI and what is the difference between the CLI and the GUI.
GUI(Graphical User Interface): we all already know what it is. when u open any application u have a window opened with a navbar, buttons and a scrolling bar, which is something u can navigate with ur mouse.
on the other hand,
CLI(Command Line Interface): is a way to interact with the computer, it is a text based interface, we can type commands in the terminal and execute them.
- example: let’s say we have a python file we need to run. with the GUI we open vscode → we click the
runbutton → the terminal opens and done.
if we want to do the same work with the terminal, we cd to the directory where the py file is located → then we type python filename.py → then Enter and we are done.
note: the
cdcommand is used to change the directory, we will learn it in Chapter 3.
this is the main difference between the CLI and the GUI. the CLI is a text based interface where we type commands and execute them, the GUI is graphic based and can be navigated with the mouse.
3. some words that need to be differentiated
-
Terminal: the first time the word Terminal was used, it was for a physical device (a too old device). it was like a computer that had its own keyboard u can use to interact with it, and on the screen the very familiar black terminal screen with its white command line. but nowadays we use the word Terminal to refer to the terminal emulator programs. -
Terminal Emulator: is basically a software program that emulates the behaviour of the old physical terminal device. the most known ones areAlacrittyandghostty. it can accept commands and render text on the screen. -
Shell: is what happens behind the scenes. when we write a command in the terminal emulator, the shell interprets it and executes it then returns the result. it works with a very basic idea calledREPLwhich is Read → Eval → Print → Loop, too ez. examples of shells:Bash,ZshandFish.
and this is the right moment to say it, because the next section is where it starts to matter: there is more than one shell, and they do not all speak the same language. this course teaches
bash.
echo $SHELL/usr/bin/fish
if that does not say
bash, typebashand press Enter before u continue. u are now inside a bash session and everything in this course works as written.exitputs u back to ur normal shell.
- i use
fishmyself, and in fish the very next section already fails.name="ati"gives ufish: Unsupported use of '='. so this is not something to skip. - the full explanation is in the course README, under “before u start”.
4. how to write variables in the terminal
in python we can use
=to assign a value to a variable, the same in the terminal.
name="ati"
echo $nameati
so we assign the value with
=and we read it back by putting$in front of the name.
- note: no space before or after the
=.name = "ati"is wrong, the right one isname="ati".
5. the two kinds of quotes
u just saw that
$namegets replaced by its value. so now there is one thing u need to know before it bites u, because it will.
double quotes let the
$work. single quotes do not.
name="ati"
echo "hello $name"hello ati
echo 'hello $name'hello $name
look at the second one. with the single quotes nothing was replaced, u got
$nameback exactly as u typed it. with the double quotes u got the value.
- so the rule is short: double quotes when u want the variable, single quotes when u want the text itself.
it looks like a small detail now. it is not, and u will meet it again in almost every chapter after this one.
and one more thing about quotes while we are here. if a file name has a space in it, u have to quote it, otherwise the terminal thinks u are talking about 2 different things:
touch my file.txt
lsfile.txt my
that made 2 files,
myandfile.txt, which is not what we wanted. so:
touch "my file.txt"
lsmy file.txt
- this is also the reason u will see developers avoid spaces in file names completely and write
my-file.txtormy_file.txtinstead. it is just less to think about.
NOTES
these are small commands and tricks u will use all the time, they are not a topic on their own but u should know them.
- the
Tabkey: this is the most useful key in the whole terminal and nobody tells u about it.
u start typing a file or a folder name, u press
Tab, and the terminal finishes it for u.
cd Doc<Tab>cd Documents/u don’t type long paths. u type 3 letters and press
Tab.
- if more than one thing matches, nothing happens on the first press. press
Tabagain and it shows u the choices:
cd Do<Tab><Tab>Documents/ Downloads/
then u type one more letter to make it clear which one, and press
Tabagain.
- it works on commands too, not only files. type
chmthenTaband u getchmod. - and it saves u from typos. if
Tabdoes not complete the name, that usually means the file is not there or u are not in the folder u think u are in. it is a free spell check.
ctrl + r: search backwards in ur history.
the arrow keys are fine for the last 3 commands. but when the command u want was 40 commands ago, u press
ctrl + rand start typing any part of it:
(reverse-i-search)`grep': grep -rn "hello" ./notes
keep pressing
ctrl + rto go further back through the matches, and press Enter to run the one u found, or the arrow keys to edit it first.
history: shows u the last commands u used in ur terminal.
history 1 echo "Hello World"
2 name="ati"
3 echo $name
4 whoami
5 history
whoami: shows u the username of the current user.
whoamiati
-
the arrow keys: u can use the up and down arrows to go through the previous commands in ur history, so u don’t type the same long command again.
-
clear: clears the screen of the terminal.
clearAssignment
- Assign ur name, surname, age and ur job to variables.
- Print each variable with
echo. - Try to write this sentence in the terminal using ur variables: “Hello everyone! My name is Mohamed Ati. I am 23 years old, and I work as a programmer.”
- Use
historyto find the first command u typed in this chapter. - Add more variables and print them together in one line.
- Print the sentence
my variable is called $nameon the screen, with the$nameshowing as text and not being replaced. - Create a file with a space in its name, then delete it. Both times, quote it properly.
- Use
Tabto move into urDocumentsfolder without typing the whole word. - Use
ctrl + rto find and re-run a command u typed earlier in this chapter.
stuck, or done and want to check? the solutions are here