Lecture Identity
File/lecture name: The shell and bash (Rob Hackman, Winter 2025, University of Alberta)
Main theme: Introduction to Operating System interfaces, Shell capabilities, and I/O redirection mechanisms.
Prereqs assumed by slides: Basic familiarity with file systems, command line usage, and hardware resources.
What this lecture enables you to do:
- Understand the fundamental purposes of an operating system (resource management, security, file access).
- Define and utilize a Shell as a textual interface for system tasks.
- Chain programs together using pipes to process data streams.
- Redirect standard error and input streams to manage program output and file sources.
- Read and modify file execute/read/write permissions with
ls -l and chmod.
- Connect to remote systems using
ssh username@serverAddress.
- Write and execute basic Bash scripts using shebang, conditionals, and loops.
- Locate and execute system utilities using the PATH environment.
Big Picture Map (High-Level)
- Operating System Fundamentals: Review of OS purposes (hardware management, security, file access).
- Shell Definition: Introduction to the Shell as a textual interface for tasks (run programs, manage files, terminate programs).
- Program Execution and PATH: How command lookup works and when full file paths are needed.
- File Permissions: Read/write/execute bits,
ls -l, and permission changes with chmod.
- Interpreters and Executable Scripts: Shebang basics and why execute permission is required.
- Working Over a Network: SSH fundamentals and the
ssh username@serverAddress command pattern.
- Bash Scripts: Script structure, simple conditionals, and
for loops for automation.
- Variable Handling: Best practices for temporary files (
mktemp) and variable expansion ($() vs $).
- Pipes: Mechanism for passing output of one program as input to another (Unix philosophy of small single-purpose programs).
- Standard Streams: Overview of
stdin, stdout, and stderr default connections.
- Redirection: Overriding default connections using
> and 2> to write to files.
- Input Methods: Comparing command line arguments vs. input redirection (
<).
Key Concepts & Definitions
Shell
- Definition: A textual interface allowing users to ask the operating system to perform tasks.
- Why it matters: It acts as the bridge between the user and the OS kernel, enabling program execution and file management.
- Common confusion: Confusing the Shell (interface) with the Operating System (kernel/hardware manager).
- Tiny example:
ls (Run a program via shell).
Pipe (|)
- Definition: A mechanism to provide the output of one program as the input to another.
- Why it matters: Enables chaining small, single-purpose programs to solve complex tasks (Unix philosophy).
- Common confusion: Thinking pipes connect files directly; they connect program streams.
- Tiny example:
head -20 alice.txt | wc.
Standard Error (stderr)
- Definition: A default stream for error messages, distinct from standard output (
stdout).
- Why it matters: Allows error messages to be captured separately from normal program output.
- Common confusion: Assuming all output goes to the screen; errors can be redirected independently.
- Tiny example:
wc 2> errorFile.txt.
PATH
- Definition: A variable indicating where the system looks for executable programs.
- Why it matters: Determines where commands like
wc or ls are located on the machine.
- Common confusion: Thinking commands are magic; they are utilities provided with the OS (e.g., Ubuntu).
- Tiny example:
wc alice.txt.
Input Redirection (<)
- Definition: Feeding a file as input to a command instead of typing arguments.
- Why it matters: Allows processing of large files without passing them as arguments.
- Common confusion: Output difference between argument and redirection (slides note output is "slightly different").
- Tiny example:
wc < /usr/share/dict/words.
Core Mechanics / How-To
How to use a Pipe
- Identify the command that produces the data (left side).
- Identify the command that consumes the data (right side).
- Combine them with the pipe character
|.
- Syntax:
cmd1 | cmd2 | cmd3.
- Decision Point: Ensure the left command writes to stdout and the right reads from stdin.
How to Redirect Standard Error
- Identify the command producing errors (e.g.,
wc).
- Use
2> followed by the target filename.
- Syntax:
command 2> filename.
- Warning: The operating system creates the file; it will overwrite (delete) any existing file with that name.
How to Read from a File as Input
- Identify the command expecting input.
- Use
< followed by the filename.
- Syntax:
command < filename.
- Note: This differs from passing the filename as an argument (e.g.,
wc file.txt vs wc < file.txt).
How to Handle Temporary Files Safely
- Use
mktemp to create a temporary file.
- Store the result in a variable (e.g.,
myTemp).
- Use
$() when running the command to capture output into the variable.
- Use
$ when reading the variable later.
- Crucial Step: Ensure you delete the temp file with
rm to avoid clutter.
Code Patterns & Idioms
Pattern: Command Chaining
- When to use it: When the output of one tool is needed as input for another.
- Correct minimal example:
head -20 alice.txt | wc.
- Common bug: Forgetting the pipe character, resulting in two separate commands running sequentially.
Pattern: Error Redirection
- When to use it: When you want to log errors to a file instead of the terminal.
- Correct minimal example:
wc 2> errorFile.txt.
- Common bug: Using
> instead of 2>, which redirects standard output (data) instead of errors.
Pattern: Input Redirection
- When to use it: When a command expects input from a file rather than arguments.
- Correct minimal example:
wc < /usr/share/dict/words.
- Common bug: Confusing this with
wc /usr/share/dict/words (argument passing), which may yield different output behavior.
Pitfalls, Edge Cases, and Debugging
- File Overwrite:
2> filename creates the file if it doesn't exist, but overwrites it if it does.
- Symptom: Data loss in
filename.
- Cause: Existing file with same name.
- Fix: Check if file exists before redirecting or use
>> (append) if appropriate (not covered in slides, but implied risk).
- Variable Expansion: Using
$ vs $() changes how output is captured.
- Symptom: Command fails or variable is empty.
- Cause: Incorrect expansion syntax for command substitution.
- Fix: Use
$() for command output, $ for variable content.
- Temp File Cleanup: Failing to delete
mktemp files.
- Symptom: Disk space usage increases over time.
- Cause:
rm command omitted.
- Fix: Always run
rm on the temp variable.
- Input Method Difference:
wc file vs wc < file.
- Symptom: Output is "slightly different" (as per slides).
- Cause: Argument parsing vs stream redirection differences.
- Fix: Understand which method the specific utility expects.
- Standard Stream Confusion: Mixing up
stdout and stderr.
- Symptom: Errors appear on screen instead of file.
- Cause: Using
> instead of 2>.
- Fix: Use
2> for errors specifically.
- PATH Issues: Commands not found.
- Symptom:
command not found.
- Cause: Program not in PATH or not installed.
- Fix: Check PATH or install utility (e.g., Ubuntu utilities).
- Pipe Chain Breakage: One command in a chain fails.
- Symptom: Downstream commands receive no input.
- Cause: Upstream command error or empty output.
- Fix: Check exit status of upstream commands.
- File Permissions: OS manages access to resources.
- Symptom: Cannot read/write files.
- Cause: OS security environment.
- Fix: Check file permissions (covered in "Executing Programs and File Permissions" section).
- Variable Scope: Shell variables vs environment variables.
- Symptom: Variable not accessible in subshells.
- Cause: Local vs global scope.
- Fix: Export variables if needed (not explicitly covered, but implied by "environment").
- Redirection Overwrite Risk: Overwriting critical system files.
- Symptom: System error.
- Cause: Redirecting to a system file path.
- Fix: Use unique filenames for redirection.
- Input Stream Closure: Closing stdin prematurely.
- Symptom: Program waits for input.
- Cause: Input redirection not set up.
- Fix: Use
< to provide input source.
- Command Substitution Syntax: Mixing
$ and $().
- Symptom: Syntax error or empty variable.
- Cause: Using
$ for command output.
- Fix: Use
$() for command output.
- Temp File Persistence: Leaving temp files behind.
- Symptom: Disk full.
- Cause:
mktemp result not deleted.
- Fix:
rm the variable content.
- Standard Error Capture: Capturing errors to wrong stream.
- Symptom: Errors mixed with output.
- Cause: Redirecting
1> instead of 2>.
- Fix: Use
2> for errors.
- File Creation Behavior: OS creates file on redirect.
- Symptom: Unexpected file creation.
- Cause:
2> creates file if missing.
- Fix: Be aware of side effects of redirection.
Exam-Style Questions (with answers)
- Concept Check: What is the primary purpose of a Shell?
- Answer: To provide a textual interface for users to ask the OS to perform tasks (run programs, manage files).
- Predict-the-output: What happens if you run
wc 2> errorFile.txt?
- Answer: Standard error from
wc is sent to errorFile.txt instead of the screen. The file is created/overwritten.
- Trace Reasoning: Why did we store the result of
mktemp in a variable?
- Answer: To reference the temporary file path later (e.g., for deletion or processing).
- Bug Finding: Why is
wc < file different from wc file?
- Answer: The slides note the output is "slightly different" between input redirection and command line arguments.
- Design Question: How do you chain three commands to process data?
- Answer: Use
cmd1 | cmd2 | cmd3.
- Concept Check: What does the pipe character
| do?
- Answer: It redirects the standard output of the left command to the standard input of the right command.
- Predict-the-output: What happens to an existing file named
errorFile.txt when using 2> errorFile.txt?
- Answer: It is overwritten (deleted) by the operating system.
- What goes wrong here? What is the issue with
wc $myTemp vs wc $()myTemp?
- Answer:
$() is for command substitution (running a command), $ is for variable expansion. Using $() without a command is invalid.
- Design Question: Why does Unix advocate for small single-purpose programs?
- Answer: To combine them with other programs to solve many tasks using pipes.
- Concept Check: Where do Ubuntu utilities typically exist?
- Answer: Somewhere on the machine, located via the PATH.
- Trace Reasoning: If
head -20 alice.txt | wc is run, what is the input to wc?
- Answer: The first 20 lines of
alice.txt (stdout of head).
- Bug Finding: Why might a temp file persist on disk?
- Answer: The
rm command was not executed to delete the file created by mktemp.
Quick Reference / Cheat Sheet
- Pipe:
cmd1 | cmd2 (Output of 1 -> Input of 2).
- Stdout Redirection:
cmd > file (Write output to file).
- Stderr Redirection:
cmd 2> file (Write errors to file).
- Input Redirection:
cmd < file (Read input from file).
- Command Substitution:
$() (Capture command output into variable).
- Variable Expansion:
$var (Use stored value).
- Temp File:
mktemp (Create), rm (Delete).
- Unix Philosophy: Write small programs, combine with pipes.
- File Overwrite:
2> file creates/overwrites file.
- PATH: Location of system utilities.
Mini Glossary
- Shell: Textual interface to the operating system.
- Pipe (
|): Connects stdout of one program to stdin of another.
- Stdin: Standard input stream.
- Stdout: Standard output stream.
- Stderr: Standard error stream.
- Redirection: Overriding default stream connections.
- PATH: Environment variable for program locations.
- mktemp: Command to create temporary files.
- Variable: Storage for data values in the shell.
- Utility: Small single-purpose program (Unix philosophy).
- Overwrite: Deleting existing file content when creating a new one.
- Command Substitution: Capturing output into a variable.
- Input Redirection: Feeding a file as input to a command.
- Operating System: Manages hardware, security, and files.
- File Permissions: Controls access to resources.
- Ubuntu: The operating system used in this course context.
- CMPUT 274: Course context mentioned in slides (ignored for content).
- Terminal: Interface for shell commands.
- Stream: Data flow channel (stdin/stdout/stderr).
- Argument: Data passed to a command.
What This Lecture Does NOT Cover (Boundary)
- C++ Programming: Not covered in these slides (Course description mentions C++, but slides focus on Shell).
- Object-Oriented Programming: Not covered in these slides.
- Graph Algorithms: Not covered in these slides.
- Dynamic Programming: Not covered in these slides.
- Client-Server Computing: Not covered in these slides.
- Recursion: Not covered in these slides.
- Advanced Bash Scripting: Only basic commands and redirection are covered.
- Bash Robustness Features: Strict mode, traps, and advanced script debugging are not covered.
- Secure File Transfer Tools: Tools like
scp and rsync are not covered.
Slide Anchors (Traceability)
- OS Purposes: (Citation 1, Section 2) "Manage and provide interfaces for access to your resources (physical hardware) ... Provide environment to run programs safely and securely".
- Shell Definition: (Citation 1, Section 3) "A shell is a textual interface to allow users to ask their operating system to perform tasks".
- Shell Tasks: (Citation 1, Section 3) "Run a program, Create, read, or delete a file, Terminate a program".
- mktemp Variable Usage: (Citation 2, Section 32) "Why did we have to store the result of mktemp in a variable? ... Note that we made sure to delete the temp file we created with rm!".
- Variable Expansion: (Citation 2, Section 32) "Why did we use $() when running the mktemp command but $ when reading myTemp?".
- Pipes Concept: (Citation 2, Section 33) "Needing to provide the output of one program as the input to another is a common problem".
- Unix Philosophy: (Citation 2, Section 33) "advocate for writing small single-purpose programs which can then be combined with other programs".
- Pipe Syntax: (Citation 2, Section 33) "The general form of this is $ cmd1 | cmd2".
- Pipe Behavior: (Citation 2, Section 33) "The standard output of a command on the left-hand side of a pipe will be redirected as the standard input to the command on the right-hand side".
- Standard Error Redirection: (Citation 3, Section 16) "Redirect stderr with a two followed by the greater than sign (2>) followed by the filename".
- Stderr File Creation: (Citation 3, Section 16) "The operating system also creates the file errorFile.txt, which will overwrite (delete) any existing file".
- Input vs Arguments: (Citation 3, Section 16) "Consider the following two commands $ wc /usr/share/dict/words $ wc < /usr/share/dict/words ... output is slightly different".
- PATH: (Citation 2, Section 35) "The programs we've used so far have been utilities provided with Ubuntu and are located somewhere on our machine".