Shell Tools and Scripting
MIT 6.NULL The Missing Semester of Your CS Education, lecture 2, explained in plain English
Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.
Learn this lecture freeWatch or read the original lecture: MIT OpenCourseWare.
Part 1
This part introduces bash shell scripting, the basic building blocks of variables, quotes, exit codes, and scripts versus functions.
Key ideas
- A shell script lets you run a series of commands with control flow, such as conditionals and loops. Bash is the most common scripting language for this, and it is built for shell tasks like pipelines and reading input.
- To set a variable in bash, write foo=bar with no spaces around the equals sign, then read it with $foo. Spaces matter because the shell splits words on spaces, so foo = bar would try to run a program called foo.
- Every command reports an exit code: 0 means it worked, and any other number means an error. You can use && and || to run the next command only when the previous one succeeded or failed.
- Functions and scripts differ in an important way. A function runs inside your current shell, so it can change things like your current directory, but a script runs in its own process and cannot.
Words to know
- Exit code
- A number a command gives back when it finishes, to say whether it worked. Zero means success and anything else means something went wrong.
- Command substitution
- Running a command inside $( ) so its printed output is put into the spot where the command was written.
- Shebang line
- The first line of a script that starts with #! and names the program that should run the script, such as python.
- Shell globbing
- Using wildcard characters like ? and * in a file name so the shell fills in all the matching file names for you.
Example
Think of a wildcard like a search pattern for file names. If a folder holds files named foo, foo1, foo2, foo10, and bar, then the command rm foo? removes only foo1 and foo2, because ? matches exactly one character. The shell does the matching before the command runs.
3 checked practice questions for this part are in the app.
Part 2
This part teaches how to find files, find text inside files, find old commands, and move between folders quickly from the command line.
Key ideas
- When you forget how a command works, you can run it with -h or --help, read its manpage with man, or check a tldr page, which shows short examples of common uses.
- The find tool searches for files by name, type, size, or modification time, and it can also act on the files it finds. The fd tool does the same job with simpler, friendlier syntax.
- The locate tool searches a saved index that is refreshed about once a day, so it is faster than find but can miss files that changed recently.
- The grep tool searches inside files for a pattern, and rg (ripgrep) is a fast, easy-to-use version of it. To find a command you typed earlier, use the up arrow, history, or Ctrl+R.
- Tools like fasd and autojump remember the folders you use most and recently, so you can jump to them with a short piece of their name, such as z cool or j cool.
Words to know
- recursively
- going into every folder inside a folder, and then into the folders inside those, all the way down.
- manpage
- a built-in instruction manual for a command, opened with the man command.
- frecency
- a score that mixes how often you use a folder with how recently you used it.
- cron
- a scheduler that runs a task automatically at set times, such as once a day.
Example
Think of find as walking through every shelf of a library to look for a book, which takes time but is always up to date. The locate tool is like checking the library's card catalog, which is quick but may be a day old if new books were added since it was last updated.
3 checked practice questions for this part are in the app.
Part 3
This part gives you practice tasks for listing files, saving a directory shortcut, retrying a flaky command, and zipping files found by search, all from the command line.
Key ideas
- The ls command can be customized to show all files, including hidden ones, with sizes in easy-to-read units and files sorted from newest to oldest. The task asks you to read the manual page for ls to find the right options.
- You can write bash functions named marco and polo. Running marco saves the folder you are in, and running polo takes you back to that saved folder from anywhere. Put the code in a file called marco.sh, and use source marco.sh to load it into your shell again after each change.
- To debug a command that fails only sometimes, you can write a script that keeps running it until it fails, saving its output and errors to files. The lesson also suggests counting how many runs it took.
- Some commands, like tar, take their input as arguments rather than from STDIN, the input stream that pipes send data through. The xargs command bridges this gap by turning whatever comes in through STDIN into arguments for another command.
- When file names contain spaces, you need special handling. Use -print0 with find and -0 with xargs so each file name is kept whole. On macOS, the built-in versions of these tools may differ from the GNU versions, so you may need to install them with brew.
Words to know
- xargs
- A command that reads items from input and hands them to another command as if you had typed them as arguments.
- STDIN
- The standard input stream, the place a command reads its input from. In a pipe, the output of one command flows into the STDIN of the next.
- source
- A command that loads a file's contents into your current shell, so the functions and settings in it become available right away.
- recursively
- Working through a folder and every folder inside it, all the way down, not just the top level.
Example
Think of xargs like a helper who receives a written list of items and then carries them to a worker one at a time. The worker only needs the items, not the list itself. This is how xargs turns a stream of file names into arguments that a command like tar can use.
3 checked practice questions for this part are in the app.
← Lecture 1: Course Overview + The Shell · Lecture 3: Editors (Vim) →
Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT CSAIL · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.
LessonBound · Free lecture notes · Privacy · Terms · New York