Lecture 7: Expansions; Introduction to scripting
Overview
Burton Ma explains Bash’s seven expansion types, showing how constructs such as brace expansions, command substitutions, variables, and arithmetic expressions are replaced by their results. He then introduces Bash scripts as command sequences and demonstrates how shebang lines, executable permissions, and paths such as ./hello.sh make a script runnable.
Key takeaways
- Bash expansions replace their original syntax with generated text or values, and the expansion order matters because word splitting and filename expansion happen after earlier substitutions.
- Quote variable expansions such as "$file" when a value must remain one argument; otherwise, spaces in a filename can make commands such as unzip interpret it as several arguments.
- The default IFS separators are spaces, tabs, and newlines; temporarily setting IFS to a comma allows Bash to split comma-separated records into fields.
- Use $(command) to insert command output and $((expression)) for integer arithmetic; Bash's arithmetic expansion does not support floating-point calculations.
- A runnable script needs an interpreter directive such as #!/bin/bash or, on macOS, #!/usr/bin/env bash, plus execute permission and usually a ./ prefix when launched from its directory.
Chapters
- Brace expansion accepts comma-separated alternatives or sequence expressions, with optional literal prefixes and suffixes.
- In the nested example, an inner expansion creates alternatives such as ex and edit, which combine with UCB, lib, A, and other path components.
- Running a nested expansion is a practical way to inspect the strings Bash generates.
- A bare ~ expands to the absolute path of the current user's home directory.
- The form ~username expands to that user's home directory; Burton Ma demonstrates it with a Homer Simpson account.
- Parameter expansion handles variables and receives its own notebook because Bash variable use has additional rules.
- The syntax $(command arguments) runs a command in a subshell and replaces the substitution with its standard output.
- Burton Ma combines two date substitutions inside a double-quoted echo string to print the weekday and time.
- A variable assignment such as moo=$(cowsay moo) stores command output; use $moo to expand the stored value.
- Variable assignments must not contain spaces around the equals sign, and variable access uses a dollar sign.
- Bash arithmetic expansion uses $((expression)), with double parentheses distinguishing it from command substitution.
- Bash supports integer arithmetic, including the remainder operator: $((5 % 2)) evaluates to 1.
- Arithmetic results can be concatenated directly with text, as in dur$((3 * 5)), which forms the string dur15.
- Arithmetic expansion is uncommon for file-oriented command-line work but can be useful inside Bash scripts.
- After earlier expansions, Bash performs word splitting on unquoted results, using spaces, tabs, and newlines by default.
- Expanding a variable containing cis220 solutions.zip without quoting splits the filename into separate fields, causing unzip to look for the wrong files.
- Double-quoting a parameter expansion such as "$file" preserves the filename as one argument and prevents word splitting.
- Filename expansion occurs after word splitting, so glob patterns are handled in a later step.
- Bash stores its word-splitting characters in the IFS variable, whose default includes space, tab, and newline.
- Expanding a variable containing several whitespace-separated fields produces separate arguments; runs of whitespace act as separators.
- Setting IFS to a comma lets Bash split a comma-separated student record into fields such as student number, last name, and first name.
- Save and restore the original IFS value after custom splitting so later commands and loops retain expected behavior.
- Filename expansion, also known as globbing, runs after word splitting and matches patterns against filesystem names.
- Bash's expansion sequence includes brace, tilde, parameter, command substitution, arithmetic, word splitting, and filename expansion.
- Burton Ma identifies parameter expansion and command substitution as especially relevant for scripts, while warning that filename expansion and quoting also matter in everyday shell use.
- A Bash program is a sequence of Bash commands that the shell reads and executes; course script filenames conventionally end in .sh.
- On standard Linux installations, #!/bin/bash directs the program loader to run the script with Bash.
- On macOS, #!/usr/bin/env bash searches PATH for Bash, allowing a separately installed newer version to run instead of the system Bash.
- A shebang can name other interpreters too; Burton Ma demonstrates the same mechanism with a Python 3 script.
- Create hello.sh in a text editor, add a shebang and commands such as echo "hello world", then save the file.
- Use chmod to grant execute permission; without it, running the script produces a permission denied error.
- Run a script in the current directory as ./hello.sh because the current directory is usually not on PATH.
- Burton Ma builds a small script generator that writes a Bash shebang to temp.sh and sets the generated file's execute permission.
Transcript excerpt
0:02 Okay, good morning everybody. Um, so to remind you where we were at the end of the last class, we were in the expansions notebook. Uh, now recall exactly what an expansion is in bash. So an an expansion is some sort of construct in bash. Uh, where the expansion gets replaced by the result of performing the expansion. So we had almost finished looking at the brace expansion in bash. I'm just going to scroll down to where we were. Uh, and there's one last example. Um, so remember that a brace expansion in bash is something inside of braces. There might be a string in front of it, there might be a string after it. Um, and there inside the braces, there's either a commaepparated list of strings or there's something called a sequence
0:49 expression. Uh this particular example that I've got right uh up on the screen right now um is an example of a nested brace expansion. So you can see that there's um one expansion uh inside of a larger expansion. Um and to figure out exactly what this thing does, it's easiest just to run it and see what happens. And so what it does is it ends up outputting um in this case one two three four four different uh directory names right and if you trace through what's happening here it's not too difficult to figure out what's going on right there's a so the inner expansion oops sorry is going to produce two separate strings it's going to produce a string ex uh and it's going to produce a string edit
1:41 there's another uh there's There's a UCB here, right? So that shows up. Oop, sorry. There's a user UCB. Oh, sorry. There's a UCB here, and there's a lib here. Uh, and then there's another A and
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Burton Ma.