Lecture 1: The Shell

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# machine: missing
# current working directory: ~(home)
# $: tells that you are not the root user
missing:~$ date
Fri Dec 17 16:06:49 JST 2021

# shell parses the command by whitespace
missing ~$ echo hello
hello

# either use quotes or escape characters
missing ~$ echo "Hello World" # single quote is also ok
Hello World
missing ~$ echo Hello\ World
Hello World

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
2
3
4
5
6
7
# the shell searches through the ":"-separated list of directories for a file named echo
missing:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# find out which file is executed for a program name: using "which" program
missing:~$ which echo
/bin/echo

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
missing:missing$ pwd
/home/missing

missing:~$ cd /home
missing:/home$ pwd
/home

missing:/home$ cd ..
missing:/$ pwd
/

missing:/$ cd ./home
missing:/home$ pwd
/home

missing:/home$ cd missing
missing:missing$ pwd
/home/missing

# cd to the directory you were previously in
missing:/home$ cd -
/home

missing:~$ ../../bin/echo hello
hello

Commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
missing:/$ ls
bin
boot
dev
etc
home
...

# -l: usee a long listing format
# r: for files, it means read contents; for directories, it means see which files are in the directory;
# w: for files, it means write or revise contents of the file; for directories, it means rename/create/remove files within this directory
# x: for files, it means execute the file; for directories, it means search or enter the directory
missing:~$ ls -l /home
drwxr-xr-x 1 missing users 4096 Jun 15 2019 missing

The paramter -l gives us more information about each file or directory.

  • The first d tells that missing 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
2
# press "q" to exit
missing:~$ man ls

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
missing:~$ echo hello > hello.txt

missing:~$ cat hello.txt
hello

missing:~$ cat < hello.txt
hello

missing:~$ cat < hello.txt > hello2.txt
missing:~$ cat hello2.txt
hello

# >> operator: append to a file
missing:~$ echo hello2 >> hello2.txt

# pip character |: chain promgrams such that the output of one is the input of another
missing:/$ ls
bin
boot
dev
etc
home

# tail: print the last n lines of its input
missing:/$ ls | tail -n2
# etc
# home

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
2
3
4
missing:/$ sudo su
(enter password)
#change from $ to #: from non-root user to root user
missing:/#

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
2
3
4
5
6
7
8
9
# /sys/class/backlight file: expose your laptop's screen

$ sudo find -L /sys/class/backlight -maxdepth 2 -name '*brightness*'
/sys/class/backlight/thinkpad_screen/brightness

$ cd /sys/class/backlight/thinkpad_screen

# tee command: take its input and write to a file, but also to the standard output
$ echo 3 | sudo tee brightness

Exercises

My solution of exercises.