Week 2 Flashcards
What is the shell?
A shell is a program that presents a prompt and waits for text commands
Commands are typically the names of executable programs
Most commonly, the shell finds the program indicated by the command, executes it, and displays its output
Note: every time you see a line beginning with “$” in my slides, it means what follows is a shell command
Define these terms:
binaries
process
process ID (PID)
command line applications
environment variables
These files are compiled programs (typically). They are often called binaries.
Through a complicated set of actions, the OS loads the binary into memory and executes it.
An executing program/binary is called a process.
The OS stores metadata about each process, e.g., the process ID (PID).
Command-line applications often interact with each other and the user through files, e.g., file
redirection, pipes, stdin, stdout, etc.
Shells use environment variables to store configuration information, e.g., $PATH denotes where to look for binaries.
12
What are these common shells? Which is the one that we are going to use in this class?
sh:
bash:
zsh:
sh: Bourne Shell: the original UNIX shell, developed at Bell Labs in the 1970s
bash: Bourne Again Shell: GNU project extension of sh (default in Linux)
zsh: Z Shell: extended version of bash w/ different syntax (default in MacOS)
In this class we will use bash.
What is an environment variable?
A shell maintaining some metadata that alter its functioning or that of the programs that are executed.
Each key-value pair represents a variable and its value
Variables can be read and/or modified by the shell, programs, or the user.
How is metadata stored?
This metadata is stored as key-value pairs:
VAR1_NAME = VAR1_VALUE
VAR2_NAME = VAR2_VALUE
What is the variable PATH?
PATH tells the shell in which folders it should look for programs.
We can check it’s content with:
$echo $PATH
What does this command do
$echo $PATH
echo: command line utility
that prints a string to the
terminal
$VARNAME: when the shell
encouters “$”, it replaces it
with the value of the
variable that follows
Consider this scenario. Your program is not in one of the folders in PATH. Therefore you run this:
$PATH=$PATH:/path/to/my/folder
Why is this bad
Confusing: you end up not knowing from where you are executing
Bad for security: you are not executing the program you think you are
What is a working directory.
By default the location that your program will look for data.
What do the following commands do?
$./mycmd
$pwd
$cd path/to/new/working/directory
$ ./mycmd will look for a program called mycmd in the current directory and run it
Print current working directory: $ pwd
Change working directory: $ cd path/to/new/working/directory
What do the following commands do?
$ cd ../
$ ../myprog
$ cd ../ → change the current working directory to the parent
$ ../myprog → executes myprog in the parent directory
What does this command do?
$ ls -1 /dev | grep tty | wc -l
ls -1 /dev lists all files in /dev in a single column of text
grep tty lists all the lines that contain the string “tty”
wc -l counts the number of lines in the output
What does the | mean?
What’s this “|” business?
The “|” symbol represents a pipe operand
* An expression of the form “cmd1 | cmd2” means:
* Execute the cmd1 program
* Take its output
* Feed it as input to the cmd2 program
In a UNIX like system when a program starts it opens three file like objects. What are they?
Standard input (stdin): an input stream which receives input from the terminal
Standard output (stdout): an output stream which by is printed on the terminal
Standard error (stderr): like standard output, but used for error messages
What do these commands do?
grep
wc
grep: search for a string in each line of input
wc: counts the number of characters/words/lines in the input