0% found this document useful (0 votes)
4 views130 pages

2 Shell Tools

The document outlines a lecture on Shell Tools for CS 45, covering the UNIX Shell, basic commands, and the concept of pipes. It explains the history of UNIX, the philosophy behind it, and the structure of a computer, including the role of the kernel and userspace. Additionally, it discusses different types of shells across operating systems and provides examples of basic shell commands.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views130 pages

2 Shell Tools

The document outlines a lecture on Shell Tools for CS 45, covering the UNIX Shell, basic commands, and the concept of pipes. It explains the history of UNIX, the philosophy behind it, and the structure of a computer, including the role of the kernel and userspace. Additionally, it discusses different types of shells across operating systems and provides examples of basic shell commands.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS 45, Lecture 2

Shell Tools

Winter 2023
Akshay Srivatsan, Ayelet Drazen, Jonathan Kula

1
Outline

1. What is the Shell?

2. The UNIX Shell

3. Basic Commands

4. Pipes

5. Conclusion

2
Outline

1. What is the Shell?


1.1 What is an Operating System?
1.2 The UNIX Philosophy
1.3 The UNIX File Abstraction

2. The UNIX Shell

3. Basic Commands

4. Pipes
3
UNIX

• The shell (as we recognize it) began with the UNIX operating system in 1969.1
• UNIX was made at Bell Labs by Ken Thompson and Dennis Ritchie.
• UNIX introduced what is now called “the UNIX philosophy.”
• Almost all modern computing is derived from the legacy of UNIX.

1
Dennis M. Ritchie and Ken Thompson. The UNIX time-sharing system.
Commun. ACM, 17(7):365–375, jul 1974

4
UNIX

• The shell (as we recognize it) began with the UNIX operating system in 1969.1
• UNIX was made at Bell Labs by Ken Thompson and Dennis Ritchie.
• UNIX introduced what is now called “the UNIX philosophy.”
• Almost all modern computing is derived from the legacy of UNIX.

1
Dennis M. Ritchie and Ken Thompson. The UNIX time-sharing system.
Commun. ACM, 17(7):365–375, jul 1974

4
Outline

1. What is the Shell?


1.1 What is an Operating System?
1.2 The UNIX Philosophy
1.3 The UNIX File Abstraction

2. The UNIX Shell

3. Basic Commands

4. Pipes
5
Anatomy of a Computer

6
Anatomy of a Computer

Input Keyboards, Mice, Serial Ports, etc.

6
Anatomy of a Computer

Input Keyboards, Mice, Serial Ports, etc.


Output Screens, Serial Ports, Speakers, etc.

6
Anatomy of a Computer

Input Keyboards, Mice, Serial Ports, etc.


Output Screens, Serial Ports, Speakers, etc.
Storage Memory (RAM), Disks, Disc Readers, etc.

6
Anatomy of a Computer

Input Keyboards, Mice, Serial Ports, etc.


Output Screens, Serial Ports, Speakers, etc.
Storage Memory (RAM), Disks, Disc Readers, etc.
Compute CPUs (math), FPUs (math with decimals), GPUs (math with matrices)

6
Anatomy of a Computer

Input Keyboards, Mice, Serial Ports, etc.


Output Screens, Serial Ports, Speakers, etc.
Storage Memory (RAM), Disks, Disc Readers, etc.
Compute CPUs (math), FPUs (math with decimals), GPUs (math with matrices)
Networking Ethernet, Wi-Fi, Serial Ports, etc.

6
Anatomy of a Computer

Input Keyboards, Mice, Serial Ports, etc.


Output Screens, Serial Ports, Speakers, etc.
Storage Memory (RAM), Disks, Disc Readers, etc.
Compute CPUs (math), FPUs (math with decimals), GPUs (math with matrices)
Networking Ethernet, Wi-Fi, Serial Ports, etc.
Misc. Fans, Power Supplies, Sensors, etc.

6
Anatomy of a Computer

Input Keyboards, Mice, Serial Ports, etc.


Output Screens, Serial Ports, Speakers, etc.
Storage Memory (RAM), Disks, Disc Readers, etc.
Compute CPUs (math), FPUs (math with decimals), GPUs (math with matrices)
Networking Ethernet, Wi-Fi, Serial Ports, etc.
Misc. Fans, Power Supplies, Sensors, etc.

6
Anatomy of a Computer

Input Keyboards, Mice, Serial Ports, etc.


Output Screens, Serial Ports, Speakers, etc.
Storage Memory (RAM), Disks, Disc Readers, etc.
Compute CPUs (math), FPUs (math with decimals), GPUs (math with matrices)
Networking Ethernet, Wi-Fi, Serial Ports, etc.
Misc. Fans, Power Supplies, Sensors, etc.
Definition (kernel)
An Operating System Kernel is a program that abstracts over different
hardware, allowing the same software to run on different computers.
6
Userspace

• A kernel by itself is kind of useless.

7
Userspace

• A kernel by itself is kind of useless.


• Abstractions are great, but we want to do something with our computers.

7
Userspace

• A kernel by itself is kind of useless.


• Abstractions are great, but we want to do something with our computers.
• This is where USERSPACE comes in.

7
Userspace

• A kernel by itself is kind of useless.


• Abstractions are great, but we want to do something with our computers.
• This is where USERSPACE comes in.

Definition (userspace)
Userspace is the set of programs that come bundled with an OS kernel, which
allow a user to perform various tasks.

7
Running Programs

• Now that we have a bunch of programs installed, we want to run them.

8
Running Programs

• Now that we have a bunch of programs installed, we want to run them.


• We need something that wraps up all the programs and provides a common
interface to them.

8
Running Programs

• Now that we have a bunch of programs installed, we want to run them.


• We need something that wraps up all the programs and provides a common
interface to them.

Definition (shell)
A shell is the outermost layer of an operating system; it lets a user run userspace
programs, which in turn let a user interact with their computer’s hardware.

8
Running Programs

• Now that we have a bunch of programs installed, we want to run them.


• We need something that wraps up all the programs and provides a common
interface to them.

Definition (shell)
A shell is the outermost layer of an operating system; it lets a user run userspace
programs, which in turn let a user interact with their computer’s hardware.

Definition (operating system)


An Operating System is the combination of a kernel, a set of userspace
programs, and a shell.
8
Types of Shell

Operating System Shell Type How you start programs


Windows [Link] Graphical Start Menu, Desktop
macOS Aqua Graphical Dock, Launchpad
iOS, Android Home Screen Graphical Tap icon
Linux GNOME, KDE, XFCE, … Graphical Various

9
Types of Shell

Operating System Shell Type How you start programs


Windows [Link] Graphical Start Menu, Desktop
macOS Aqua Graphical Dock, Launchpad
iOS, Android Home Screen Graphical Tap icon
Linux GNOME, KDE, XFCE, … Graphical Various
Windows [Link] Text Type name of .exe file
UNIX sh Text The rest of this lecture.
Linux bash Text Same as sh
macOS zsh Text Same as sh
Table: Shells across common operating systems

9
Types of Shell

Operating System Shell Type How you start programs


Windows [Link] Graphical Start Menu, Desktop
macOS Aqua Graphical Dock, Launchpad
iOS, Android Home Screen Graphical Tap icon
Linux GNOME, KDE, XFCE, … Graphical Various
Windows [Link] Text Type name of .exe file
UNIX sh Text The rest of this lecture.
Linux bash Text Same as sh
macOS zsh Text Same as sh
Table: Shells across common operating systems

While all of these shells can start programs, only the UNIX shell (and its derivatives)
can combine them. 9
Types of Shell

Operating System Shell Type How you start programs


Windows [Link] Graphical Start Menu, Desktop
macOS Aqua Graphical Dock, Launchpad
iOS, Android Home Screen Graphical Tap icon
Linux GNOME, KDE, XFCE, … Graphical Various
Windows [Link] Text Type name of .exe file
UNIX sh Text The rest of this lecture.
Linux bash Text Same as sh
macOS zsh Text Same as sh
Table: Shells across common operating systems

While all of these shells can start programs, only the UNIX shell (and its derivatives)
can combine them.
9
Outline

1. What is the Shell?


1.1 What is an Operating System?
1.2 The UNIX Philosophy
1.3 The UNIX File Abstraction

2. The UNIX Shell

3. Basic Commands

4. Pipes
10
Original

As described in the Bell System Technical Journal in 1978 2 :


1. Make each program do one thing well. To do a new job, build afresh rather than complicate old
programs by adding new "features."
2. Expect the output of every program to become the input to another, as yet unknown, program.
Don't clutter output with extraneous information. Avoid stringently columnar or binary input
formats. Don't insist on interactive input.
3. Design and build software, even operating systems, to be tried early, ideally within weeks. Don't
hesitate to throw away the clumsy parts and rebuild them.
4. Use tools in preference to unskilled help to lighten a programming task, even if you have to
detour to build the tools and expect to throw some of them out after you've finished using them.
2
M. D. McIlroy, E. N. Pinson, and B. A. Tague. UNIX time-sharing system: Foreword.
The Bell System Technical Journal, 57(6):1899–1904, July 1978
11
Simplified

The UNIX Philosophy


Build lots of small tools, each of which does exactly one thing well, but which can be
combined to do more powerful things.

12
Outline

1. What is the Shell?


1.1 What is an Operating System?
1.2 The UNIX Philosophy
1.3 The UNIX File Abstraction

2. The UNIX Shell

3. Basic Commands

4. Pipes
13
The UNIX File Abstraction

• In UNIX, everything is a file (including hardware!).

14
The UNIX File Abstraction

• In UNIX, everything is a file (including hardware!).


• Most files are text.

14
The UNIX File Abstraction

• In UNIX, everything is a file (including hardware!).


• Most files are text.
• Programs which operate on text can operate on almost everything.

14
Outline

1. What is the Shell?

2. The UNIX Shell

3. Basic Commands

4. Pipes

5. Conclusion

15
The UNIX Shell

16
The Prompt

Example (prompt)
An default shell prompt might look like this:

[akshay@akshays-thinkpad ~]$

This PROMPT will print every time the shell is ready to accept another command. It
probably looks different on your computer, since different shells have different
defaults. Regardless of what it looks like right now, you can customize it to look like
whatever you want. In most cases, you can get back to it by pressing CTRL-C on
your keyboard.

17
The Prompt

Username
Example (prompt: username)
This part of the prompt is your username:

[akshay@akshays-thinkpad ~]$

This is probably the same as the username you use to log into your computer. By
default, you're auto-logged into the shell using your normal user account.

18
The Prompt

Hostname
Example (prompt: hostname)
This part of the prompt is your computer’s hostname:

[akshay@akshays-thinkpad ~]$

This is your computer's name on whatever network it's connected to. Generally you
don't really care about this unless you have multiple computers.

19
The Prompt

Current Directory
Example (prompt: current directory)
This part of the prompt is your current directory.

[akshay@akshays-thinkpad ~]$

This is your WORKING DIRECTORY; “directory” is just a fancy name for “folder”, like
you'd have in Windows Explorer or macOS Finder.
By default, you start in your HOME DIRECTORY, which is the folder that contains
Documents, Downloads, Pictures, Videos, etc. The home directory is abbreviated
as a tilde (~) because it's so common.
20
Outline

1. What is the Shell?

2. The UNIX Shell

3. Basic Commands
3.1 Directories
3.2 Files

4. Pipes

21
5. Conclusion
Outline

1. What is the Shell?

2. The UNIX Shell

3. Basic Commands
3.1 Directories
3.2 Files

4. Pipes

22
5. Conclusion
Listing Files

In Windows Explorer or macOS Finder, the current directory is always visible. In the
shell, you have to ask for the list of current files manually.

23
Listing Files

In Windows Explorer or macOS Finder, the current directory is always visible. In the
shell, you have to ask for the list of current files manually.
The LIST command is called ls.

23
Listing Files

In Windows Explorer or macOS Finder, the current directory is always visible. In the
shell, you have to ask for the list of current files manually.
The LIST command is called ls.
Example (ls)
On my computer, the results look like this:

[akshay@akshays-thinkpad ~]$ ls
Desktop Downloads Music Public Videos
Documents Dropbox Pictures Templates
[akshay@akshays-thinkpad ~]$

These are all the SUBDIRECTORIES of my home directory.


23
You are here

• Before we go exploring, let's do one more command in the home directory.

24
You are here

• Before we go exploring, let's do one more command in the home directory.


The PRINT WORKING DIRECTORY command is called pwd.

24
You are here

• Before we go exploring, let's do one more command in the home directory.


The PRINT WORKING DIRECTORY command is called pwd.
Example (pwd)
Print the current working directory:

[akshay@akshays-thinkpad ~]$ pwd


/home/akshay
[akshay@akshays-thinkpad ~]$

24
You are here

• Before we go exploring, let's do one more command in the home directory.


The PRINT WORKING DIRECTORY command is called pwd.
Example (pwd)
Print the current working directory:

[akshay@akshays-thinkpad ~]$ pwd


/home/akshay
[akshay@akshays-thinkpad ~]$

Definition (root directory)


The root directory is the topmost directory on the filesystem. It’s often called /.
24
Changing Directories

• The home directory isn't too interesting on its own.


• Let's go somewhere you probably know well: the Desktop!
• In Explorer or Finder you could just click on a folder to enter it.

25
Changing Directories

• The home directory isn't too interesting on its own.


• Let's go somewhere you probably know well: the Desktop!
• In Explorer or Finder you could just click on a folder to enter it.
The CHANGE DIRECTORY command is called cd.

25
Changing Directories

• The home directory isn't too interesting on its own.


• Let's go somewhere you probably know well: the Desktop!
• In Explorer or Finder you could just click on a folder to enter it.
The CHANGE DIRECTORY command is called cd.
Example (cd)
Change (cd) into the Desktop directory:

[akshay@akshays-thinkpad ~]$ cd Desktop


[akshay@akshays-thinkpad Desktop]$

25
Not Changing Directories

• There's a special name that always means “the current directory”: .


• cd . says “change directory to the current directory”.

26
Not Changing Directories

• There's a special name that always means “the current directory”: .


• cd . says “change directory to the current directory”.
Example (cd .)
Don’t change directories:

[akshay@akshays-thinkpad Desktop]$ pwd


/home/akshay/Desktop
[akshay@akshays-thinkpad Desktop]$ cd .
[akshay@akshays-thinkpad Desktop]$ pwd
/home/akshay/Desktop

26
Making Directories
• Now that we're on the desktop, let's create a new directory to do some
experiments in.
• This is the equivalent of right-clicking and selecting “New Folder”.

27
Making Directories
• Now that we're on the desktop, let's create a new directory to do some
experiments in.
• This is the equivalent of right-clicking and selecting “New Folder”.
The MAKE DIRECTORY command is called mkdir.

27
Making Directories
• Now that we're on the desktop, let's create a new directory to do some
experiments in.
• This is the equivalent of right-clicking and selecting “New Folder”.
The MAKE DIRECTORY command is called mkdir.
Example (mkdir)
Create a directory called “cs45-test-directory” and cd into it:

$ mkdir cs45-test-directory
$ cd cs45-test-directory/

You can go back to the Desktop by typing cd .. or cd ~/Desktop .


27
Making Directories
• Now that we're on the desktop, let's create a new directory to do some
experiments in.
• This is the equivalent of right-clicking and selecting “New Folder”.
The MAKE DIRECTORY command is called mkdir.
Example (mkdir)
Create a directory called “cs45-test-directory” and cd into it:

$ mkdir cs45-test-directory
$ cd cs45-test-directory/

You can go back to the Desktop by typing cd .. or cd ~/Desktop .


The REMOVE DIRECTORY command is rmdir. 27
Directory Review
Shortcut Name
~ Home Directory
/ Root Directory
. Current Directory
.. Parent Directory
Table: Directory Shortcuts

28
Directory Review
Shortcut Name
~ Home Directory
/ Root Directory
. Current Directory
.. Parent Directory
Table: Directory Shortcuts

Command Description Argument Required


ls List Directory Directory Name No, defaults to .
cd Change Directory Directory Name No, defaults to ~
pwd Print Working Directory N/A N/A
mkdir Make Directory Directory Name Yes
rmdir Remove Directory Directory Name Yes
Table: Directory Commands 28
Outline

1. What is the Shell?

2. The UNIX Shell

3. Basic Commands
3.1 Directories
3.2 Files

4. Pipes

29
5. Conclusion
Output

• Sometimes we just want to print something out, like “hello, world”.

30
Output

• Sometimes we just want to print something out, like “hello, world”.


The PRINT command is called echo.

30
Output

• Sometimes we just want to print something out, like “hello, world”.


The PRINT command is called echo.
Example (echo)
Print the text “hello, world”:

$ echo "hello, world"


hello, world
$

30
Input

• Sometimes we want to read input from the user.

31
Input

• Sometimes we want to read input from the user.


• Unfortunately, this is trickier: there are multiple commands which can be used
for input. Let's use the simplest one.

31
Input

• Sometimes we want to read input from the user.


• Unfortunately, this is trickier: there are multiple commands which can be used
for input. Let's use the simplest one.
The CONCATENATE command is called cat. It can also be used for input.

31
Input
The CONCATENATE command is called cat. It can also be used for input.
Example (cat)
Read text from the user:

$ cat
this is a test
this is a test
line 2
line 2
$

The cat command will print out whatever you type into it… forever. To get it to stop,
you can either “kill” it by pressing CTRL-C, or tell it “end of file” by pressing CTRL-D. 31
Creating Files

• There are a few different ways to create files. Let's start with the simplest.

32
Creating Files

• There are a few different ways to create files. Let's start with the simplest.
The TOUCH FILE command is called, intuitively, touch. While we don't care about
“touching” files as such, it has the handy side effect of creating files.

32
Creating Files

• There are a few different ways to create files. Let's start with the simplest.
The TOUCH FILE command is called, intuitively, touch. While we don't care about
“touching” files as such, it has the handy side effect of creating files.
Example (touch)
To create a file called “[Link]”:

$ touch [Link]
$ ls
[Link]
$

32
Renaming Files

The MOVE FILE command is called mv. It can also be used to rename files.

33
Renaming Files

The MOVE FILE command is called mv. It can also be used to rename files.
Example (mv)
To rename a file called “[Link]” to “[Link]”:

$ mv [Link] [Link]
$ ls
[Link]
$

33
Deleting Files

The REMOVE FILE command is called rm.


This is irreversible!
This command is dangerous! It does not move the file to a “trash” folder; it
permanently and irreversibly deletes it.

34
Deleting Files

The REMOVE FILE command is called rm.


This is irreversible!
This command is dangerous! It does not move the file to a “trash” folder; it
permanently and irreversibly deletes it.

Example (rm)
To remove a file called “[Link]”:

$ rm [Link]
$

34
Writing to Files

• We have a problem though: the file we created is empty. We can't do much


with a bunch of empty files.
• We can check this by running ls with a special FLAG asking for extra info
(including the file size).

35
Writing to Files

• We have a problem though: the file we created is empty. We can't do much


with a bunch of empty files.
• We can check this by running ls with a special FLAG asking for extra info
(including the file size).
Example (ls -l)
To print extra information about files:

$ ls -l
total 0
-rw-r--r-- 1 akshay akshay 0 Dec 18 11:59 [Link]

35
Writing to Files

• We have a problem though: the file we created is empty. We can't do much


with a bunch of empty files.
• We can check this by running ls with a special FLAG asking for extra info
(including the file size).
Example (ls -l)
To print extra information about files:

$ ls -l
total 0
-rw-r--r-- 1 akshay akshay 0 Dec 18 11:59 [Link]

One very useful flag which is supported by almost every command is --help. 35
Writing to Files

• Everything is a file, including the output of our commands.

36
Writing to Files

• Everything is a file, including the output of our commands.


• By default, this is called STANDARD OUTPUT, and goes to our terminal.

36
Writing to Files

• Everything is a file, including the output of our commands.


• By default, this is called STANDARD OUTPUT, and goes to our terminal.
• The shell lets us REDIRECT standard output to go to a file instead.

36
Writing to Files

• Everything is a file, including the output of our commands.


• By default, this is called STANDARD OUTPUT, and goes to our terminal.
• The shell lets us REDIRECT standard output to go to a file instead.
Example (output redirection)
To create a file called “[Link]” with the contents hello, world :

$ echo "hello, world" > [Link]


$
To append to an existing file, you can use >> instead of > .

36
Reading from Files

• Just like STANDARD OUTPUT, the input to our programs is also a file.

37
Reading from Files

• Just like STANDARD OUTPUT, the input to our programs is also a file.
• By default, this is called STANDARD INPUT, and comes from our terminal.

37
Reading from Files

• Just like STANDARD OUTPUT, the input to our programs is also a file.
• By default, this is called STANDARD INPUT, and comes from our terminal.
• The shell also lets us REDIRECT standard input to come from a file.

37
Reading from Files

• Just like STANDARD OUTPUT, the input to our programs is also a file.
• By default, this is called STANDARD INPUT, and comes from our terminal.
• The shell also lets us REDIRECT standard input to come from a file.
Example (input redirection)
To print a file called “[Link]”:

$ cat < [Link]


hello, world
$

37
I/O(/E?)

Definition (standard input)


Standard input (/dev/stdin) is the file from which a program reads its input.

38
I/O(/E?)

Definition (standard input)


Standard input (/dev/stdin) is the file from which a program reads its input.

Definition (standard output)


Standard output (/dev/stdout) is the file to which a program writes its output.

38
I/O(/E?)

Definition (standard input)


Standard input (/dev/stdin) is the file from which a program reads its input.

Definition (standard output)


Standard output (/dev/stdout) is the file to which a program writes its output.

Definition (standard error)


Standard error (/dev/stderr) is the file to which a program writes its error
messages.

38
Redirection Operators

Operator File Overwrite?


< /dev/stdin
> /dev/stdout Overwrite
>> /dev/stdout Append
2> /dev/stderr3 Overwrite
2>> /dev/stderr Append
Table: UNIX Shell Redirection Operators

3
It’s uncommon to redirect standard error, but there are some valid reasons to (which we’ll see later in
the quarter).

39
Outline

1. What is the Shell?

2. The UNIX Shell

3. Basic Commands

4. Pipes

5. Conclusion

40
Environment Variables

Some programs need configuration that's too annoying to provide as arguments


every time.

41
Environment Variables

Some programs need configuration that's too annoying to provide as arguments


every time.
Definition (environment variable)
An environment variable is a configuration value that’s set globally by a
program, which applies to itself and any other programs it runs.

41
All the Environment Variables
The ENVIRONMENT VARIABLE command env prints all the environment variables
which are currently set.

42
All the Environment Variables
The ENVIRONMENT VARIABLE command env prints all the environment variables
which are currently set.
Example (env)
To print every environment variable:

$ env
MAIL=/var/spool/mail/akshay
PWD=/home/akshay
XDG_SESSION_TYPE=wayland
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin
HOME=/home/akshay
USERNAME=akshay
[...]
42
Pipes

• Let's see how many environment variables we have!

43
Pipes

• Let's see how many environment variables we have!


• The WORD COUNT command is called wc.

43
Pipes

• Let's see how many environment variables we have!


• The WORD COUNT command is called wc.
• It has a flag --lines (or -l) which counts lines in its input instead of words.

43
Pipes

• Let's see how many environment variables we have!


• The WORD COUNT command is called wc.
• It has a flag --lines (or -l) which counts lines in its input instead of words.
Example (count environment variables with a temporary file)
We can write the output into a temporary file, and give it as input to wc:

$ env > /tmp/[Link]


$ wc --lines < /tmp/[Link]
78

43
Pipes

• Let's see how many environment variables we have!


• The WORD COUNT command is called wc.
• It has a flag --lines (or -l) which counts lines in its input instead of words.
Example (count environment variables with a pipe)
We can connect the output of env and the input of wc with a pipe:

$ env | wc --lines
78

43
Benefits of Pipes

Definition (pipe)
A pipe is a direct connection between the output of one program and the input of
another. It can be set up using the | (pipe) operator, which connects stdout of
whatever is on the left with stdin of whatever is on the right.

44
Benefits of Pipes

Definition (pipe)
A pipe is a direct connection between the output of one program and the input of
another. It can be set up using the | (pipe) operator, which connects stdout of
whatever is on the left with stdin of whatever is on the right.

Pipes are superior to temporary files for several reasons:

44
Benefits of Pipes

Definition (pipe)
A pipe is a direct connection between the output of one program and the input of
another. It can be set up using the | (pipe) operator, which connects stdout of
whatever is on the left with stdin of whatever is on the right.

Pipes are superior to temporary files for several reasons:


• They are parallel: the programs on the left and right can run at the same time.

44
Benefits of Pipes

Definition (pipe)
A pipe is a direct connection between the output of one program and the input of
another. It can be set up using the | (pipe) operator, which connects stdout of
whatever is on the left with stdin of whatever is on the right.

Pipes are superior to temporary files for several reasons:


• They are parallel: the programs on the left and right can run at the same time.
• They are lazy: the program on the right can read exactly as much data as it
needs from the program on the left.

44
More Piping

Example (the first n environment variables)


With the head command, we can extract only the first few lines from a file:

$ env | head --lines=3


MAIL=/var/spool/mail/akshay
PWD=/home/akshay
XDG_SESSION_TYPE=wayland
$

45
More Piping

Example (random numbers)


We can lazily evaluate part of an infinitely long “file” such as /dev/random:

$ cat /dev/random | hexdump | head --lines 1


0000000 4730 003c 6c22 1d16 49ef 6eff 91b2 a9f0

45
Outline

1. What is the Shell?

2. The UNIX Shell

3. Basic Commands

4. Pipes

5. Conclusion

46
Getting Help

• The shell is far more complicated than we can possibly cover in an 80-minute
lecture.

47
Getting Help

• The shell is far more complicated than we can possibly cover in an 80-minute
lecture.
• Most commands have lots of flags and options.

47
Getting Help

• The shell is far more complicated than we can possibly cover in an 80-minute
lecture.
• Most commands have lots of flags and options.
• We already talked about the --help flag, which usually gives you a brief
summary of how to use a command.

47
The System Manual

• One super-useful resource is the UNIX system manual, which is pre-installed


on most UNIX-like systems.

48
The System Manual

• One super-useful resource is the UNIX system manual, which is pre-installed


on most UNIX-like systems.
• The MANUAL command is man; it takes as an argument the name of a command,
and it displays the manual page (“man page”).

48
The System Manual

• One super-useful resource is the UNIX system manual, which is pre-installed


on most UNIX-like systems.
• The MANUAL command is man; it takes as an argument the name of a command,
and it displays the manual page (“man page”).
• If you don't know the name of a command, you can search the manual using the
command apropos (or, equivalently, man --apropos).

48
The System Manual

• One super-useful resource is the UNIX system manual, which is pre-installed


on most UNIX-like systems.
• The MANUAL command is man; it takes as an argument the name of a command,
and it displays the manual page (“man page”).
• If you don't know the name of a command, you can search the manual using the
command apropos (or, equivalently, man --apropos).
Example (man wc)
To open the UNIX manual page for the wc word-count tool:

$ man wc

48
The System Manual

• One super-useful resource is the UNIX system manual, which is pre-installed


on most UNIX-like systems.
• The MANUAL command is man; it takes as an argument the name of a command,
and it displays the manual page (“man page”).
• If you don't know the name of a command, you can search the manual using the
command apropos (or, equivalently, man --apropos).
Example (man man)
To open the UNIX manual page for the manual itself:

$ man man

48
To be continued…

• We'll continue exploring shell tools in Lecture 3: Data Manipulation and Shell
Scripting.

49
To be continued…

• We'll continue exploring shell tools in Lecture 3: Data Manipulation and Shell
Scripting.
• There's no class on Monday, January 16th (Martin Luther King Jr. Day); we'll
pick back up on Wednesday, January 18th.

49
To be continued…

• We'll continue exploring shell tools in Lecture 3: Data Manipulation and Shell
Scripting.
• There's no class on Monday, January 16th (Martin Luther King Jr. Day); we'll
pick back up on Wednesday, January 18th.
• We'll post Assignment 1 soon, but it'll only be due on Monday, January 23rd
(twelve days from now). It'll cover Lectures 2 (this one) and 3 (next week).

49
To be continued… (Continued)

• In the meantime: try doing file management from the terminal. We didn't cover
every command you'll need, so if you don't know how to do something, try
searching the manual using apropos or searching the web.

50
To be continued… (Continued)

• In the meantime: try doing file management from the terminal. We didn't cover
every command you'll need, so if you don't know how to do something, try
searching the manual using apropos or searching the web.
• Take a look at the UNIX filesystem hierarchy layout, documented in man hier.
• Use man pages, [Link] or [Link] to find out
more about shell commands.

50
To be continued… (Continued)

• In the meantime: try doing file management from the terminal. We didn't cover
every command you'll need, so if you don't know how to do something, try
searching the manual using apropos or searching the web.
• Take a look at the UNIX filesystem hierarchy layout, documented in man hier.
• Use man pages, [Link] or [Link] to find out
more about shell commands.
Be Careful!
The shell often doesn't warn you when you're doing dangerous things! Be sure to
read the man page before running commands you find on the internet. Be especially
careful with the REMOVE FILE command, rm, or when using the > (overwrite) operator.

50
Sudo

Using sudo responsibly


• Some commands require the use of sudo, the SUPERUSER DO command.

51
Sudo

Using sudo responsibly


• Some commands require the use of sudo, the SUPERUSER DO command.
• This gives that command full access to do anything on your computer!

51
Sudo

Using sudo responsibly


• Some commands require the use of sudo, the SUPERUSER DO command.
• This gives that command full access to do anything on your computer!
• Sometimes sudo won't even ask for your password!

51
Sudo

Using sudo responsibly


• Some commands require the use of sudo, the SUPERUSER DO command.
• This gives that command full access to do anything on your computer!
• Sometimes sudo won't even ask for your password!
• If you're using sudo, make sure you know what the command after it will do.

51
Sudo

Using sudo responsibly


• Some commands require the use of sudo, the SUPERUSER DO command.
• This gives that command full access to do anything on your computer!
• Sometimes sudo won't even ask for your password!
• If you're using sudo, make sure you know what the command after it will do.
• sudo is necessary for certain tasks (we'll see some in the next few lectures), but
it's always good to be careful around it.

51
Sudo Warning

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.


#2) Think before you type.
#3) With great power comes great responsibility.

52
Interesting Commands

head: Get the beginning of a file (or pipe).


tail: Get the end of a file (or pipe).
grep: Search within a file.
sed: Find-and-replace.
cut: Get a specific “column” of a file (e.g., a CSV file).
ping: Test your internet connection.
sort: Sort lines in a file.
uniq: Remove duplicate lines in a file.
exit: Exit the terminal.
53
Questions?

54
References

[1] M. D. McIlroy, E. N. Pinson, and B. A. Tague. UNIX time-sharing system:


Foreword. The Bell System Technical Journal, 57(6):1899--1904, July 1978.
[2] Dennis M. Ritchie and Ken Thompson. The UNIX time-sharing system.
Commun. ACM, 17(7):365–375, jul 1974.

55

You might also like