Introduction to the Terminal
Introduction to the terminal/shell
Nearly all platforms you can get your hands on have a shell in one form or another, and many of them have several shells for you to choose from. While they may vary in the details, at their core they are all roughly the same: they allow you to run programs, give them input, and inspect their output in a semi-structured way.
-- Excerpt from the Missing Semester
For the most part, when people talk about the terminal, we normally talk about Bash, or POSIX-compliant shells. POSIX is just a fancy name for a set of rules that a shell should abide by, so that different shells can have similar behaviour. Other shells include:
fish
zsh
nushell
powershell
In modern operating systems, to open a shell prompt, you often need a terminal. Think of it as a nice GUI wrapping the textual interface (the shell). Your device should probably be shipped with one, or you should be able to install one easily.
The Shell Prompt
When you first launch a terminal, you will see a prompt, similar to or a slight variation of:
This prompt tells you for example, that your username is chun
on the machine named legion
, and that you 'working directory' is ~
(short for home, we'll get to that). You should also see a blinking cursor, which you can type anything to, and when you hit Enter
, it should execute the command you've typed out.
Here are some really simple commands you should be able to run (but some may not be installed depending on your machine):
date
- shows the date
cal
- a tiny calendar
uptime
- shows how long your computer has been powered on
echo
- echoes what you typed
Folders and directories
These commands are neat, but we can't really do much in the command line until we understand the concept of folders and directories.
All your files and directories on your system are stored in a structure known as a tree. This tree starts from a 'root' directory, this will be /
on Linux and MacOS, and something like C:\
on Windows.
A path on the shell is just a list of directories, seperated by /
on Linux and macOS and \
on Windows. For example:
There are two types of paths:
The absolute path is a path that starts from the root directory
Relative paths are relative to your current working directory, or where you shell currently is
To see where you current directory is, use print working directory, or pwd
for short in the terminal.
In a path, .
refers to the current directory, and ..
refers to the parent directory.
Common Commands
to get the manual pages of a command
to change directory
to list files and directories
to remove files and directories
to copy file
to move file
to print working directory
Navigating the shell
bash
has shortcuts that are based on emacs
keybindings:
Ctrl
+a
- beginning of lineCtrl
+e
- end of lineAlt
+b
- move back one wordAlt
+f
- move forward one wordCtrl
+k
- delete from cursor to end of lineCtrl
+_
- undo
And some special keybindings:
Ctrl
+u
- delete from cursor to the start of lineCtrl
+w
- delete from cursor to the start of wordCtrl
+c
- terminates the commandCtrl
+z
- suspends the commandCtrl
+l
- clears the screenCtrl
+s
- stops the output to the screenCtrl
+q
- allows output to the screen
You can find even more by doing man readline
Editing in the terminal
There are a few ways you can make changes to your filesystem, be it editing files or directories/folders:
mkdir
to make a new empty directorytouch <filename>
to make a new empty filenano <filename>
to open a editor to edit the fileCtrl
+o
- to saveCtrl
+x
- to exit
Finding commands
What if we want to find our previously used commands? If you haven't already realized, you should be able to use the up arrow to scroll through your previous commands, but it's not very efficient. This is where we can use a command called history
.
Composability
Remember what we said about the Unix Philosophy? A big part about it is the idea of programs working well together. The terminal allows this by allow the output of one program to be the input of another program. This is known as piping. To pipe we can do something like:
history
prints out the entire command history as the outputgrep
takes in an input and tries to filter for the keyword "echo"
Some other ways you could use pipes:
There are more ways we can compose programs, which we'll go through in the scripting section!
Last updated