Command-line Environment
/ 3 min read
Table of Contents
Notes for The Missing Semester Lecture 2
1. Shell Globbing & Expansion
Wildcards and expansions help you select files or generate arguments efficiently.
-
*(Wildcard): Matches zero or more characters of any type. -
Example:
*.pymatches all Python files. -
?(Single Character): Matches exactly one character of any type. -
Example:
file?.txtmatchesfileA.txtbut notfile10.txt. -
{}(Brace Expansion): Expands a comma-separated list of patterns into multiple arguments. -
Example:
convert image.{png,jpg}expands toconvert image.png image.jpg. -
Example:
touch foo{1,2,3}createsfoo1,foo2, andfoo3.
2. Quoting & Variables
Understanding how the shell interprets text is crucial for writing scripts.
-
Difference between
' 'and" ": -
Single Quotes (
' '): Preserves the literal value of each character. Variables are not expanded. -
Double Quotes (
" "): Preserves the literal value of most characters, but allows for variable expansion (interpreting$VAR) and escape characters. -
Environment Variables (
export): -
Use
exportto modify the current environment. -
Key concept: Variables set with
exportare inherited by all child processes (commands you run from that shell).
3. Streams & Redirection
Tools for manipulating input and output streams.
-
Process Substitution
<( CMD ): -
Executes
CMDand places the output in a temporary file (via a named pipe), then substitutes<()with that file path. -
Example:
diff <(ls src) <(ls docs)compares the file lists of two directories without creating intermediate files. -
FZF (Fuzzy Finder):
-
A command-line fuzzy finder. It reads lines from
stdinand provides an interactive interface to filter and select items.
4. Process Management & Job Control
Managing programs that are executing simultaneously.
-
Signals:
-
SIGINT (Ctrl+C): Interrupts the process (requests it to stop).
-
SIGQUIT (Ctrl+): Quits the process (often produces a core dump).
-
SIGTERM: Terminate signal (polite kill request).
-
SIGSTOP (Ctrl+Z): Pauses a process.
-
Job Control Commands:
-
jobs: Lists unfinished jobs associated with the current terminal session. -
fg: Brings a paused/background job to the foreground. -
bg: Resumes a paused job in the background. -
nohup: (No Hang Up) Runs a command immune to hangups, allowing it to keep running even if you close the terminal.
5. Remote Machines (SSH)
Securely connecting to and managing remote servers.
-
Key Generation:
-
ssh-keygen: Generates a public/private key pair. -
Recommended command:
ssh-keygen -a 100 -t ed25519 -f ~/.ssh/id_ed25519(uses the modern Ed25519 algorithm). -
Extract Public Key:
ssh-keygen -y -f /path/to/private_keyprints the public key derived from a private key. -
Authentication:
-
Client side: You hold the private key.
-
Server side: Looks into
~/.ssh/authorized_keysto check if your public key is listed and allowed to connect. -
Configuration:
-
~/.ssh/config: Allows you to define aliases (e.g.,ssh my-server) and default settings (user, port, identity file) for hosts. -
File Transfer:
-
rsync: A tool for fast, versatile, remote (and local) file-copying. It only transfers differences between the source and destination.