This is the lecture note of The Missing Semester - Lecture 1.
Today's topic is the shell.
What is the shell?
Computers have a variety of interfaces for giving commands:
- GUIs
- Voice Interfaces
- AR/VR
- Shell: old-school textual interfaces
- ...
The core functionality of the shell: allows you to run programs, give them input, and inspect their semi-structured output.
Using the shell
🦁 The shell showed in this blog is BASH
, which is one of the most widely used shells, and has similar syntax to many other shells.
1 | # machine: missing |
How does the shell know how to find the date
and echo
program?
Actually, the shell is a programming environment, just like Python or Ruby. So it has variables, conditionals, loops, and functions. When you run commands in your shell, you are writing a small bit of code that your shell interprets. If the shell is asked to execute a command that doesn't match one the its programming keywords, it will consult an environment variable
called $PATH
that lists which directories the shell should search for programs.
1 | # the shell searches through the ":"-separated list of directories for a file named echo |
Navigating in the shell
A path on the shell is a delimited list of directories, separated by:
- Linux and MacOS:
/
- Windows:
\
On Linux and MacOS, the path /
is the "root" of the file system, under which all directories and files lie. On Windows, there is one root for each disk partition, such as C:\
.
A path can be either:
- absolute path: start with
/
- relative path
1 | missing:missing$ pwd |
Commands
1 | missing:/$ ls |
The paramter -l
gives us more information about each file or directory.
- The first
d
tells thatmissing
is a directory; if it is a file ,then use-
parameter; rwx
indicates what permissions the owner of the file(missing
) have;r-x
indicates what permissions the owning groups(users
) have,-
means the given principal does not have the given permission;r-x
indicates what permissions everyone else has;
If you ever want more information about a program's arguments, input, outputs, or how it works in general, use man
program.
1 | # press "q" to exit |
Connecting programs
In the shell, programs have 2 primary "streams", input stream and output stream.
Normally, a program's input and output are both your terminal —— keyboard as input and screen as output. However, we can rewire streams.
The simplest form of redirection is < file
and > file
, which will let you rewire the input and output streams of a program to a file respectively.
1 | missing:~$ echo hello > hello.txt |
A versatile and powerful tool
The "root" user if a special user on most Unix-like systems.
The root user is above (almost) all access restrictions, and can create, read, update, and delete any file in the system. You will not usually log into your system as the root user though, since it’s too easy to accidentally break something. Instead, you will be using the sudo
command(program). As its name implies, it lets you “do” something “as su” (short for “super user”, or “root”). When you get permission denied errors, it is usually because you need to do something as root. Though make sure you first double-check that you really wanted to do it that way!
1 | missing:/$ sudo su |
Let's look an example.
If you want to write to the sysfs
file system mounted under /sys
. sysfs
exposes a number of kernel parameters as files, so that you can easily reconfigure the kernel on the fly without specialized tools.
Note that sysfs
does not exist on Windows or macOS.
1 | # /sys/class/backlight file: expose your laptop's screen |
Exercises
My solution of exercises.