Video summary
The video provides a detailed overview of how the shell processes input lines before executing commands, focusing on various types of expansion mechanisms. It begins by explaining that the shell uses spaces as delimiters to separate commands, options, and arguments, while ignoring extra whitespace. The primary function discussed is pathname expansion, where wildcard characters are evaluated and replaced with matching file names prior to command execution. If no files match a specific wildcard pattern, the text remains unexpanded, ensuring that the shell handles file references accurately based on the current directory contents.
Beyond wildcards, the tutorial covers several other expansion types essential for efficient scripting and file management. Tilde expansion allows users to reference their own home directory or those of other users by preceding a username with a tilde character. Brace expansion is demonstrated as a powerful tool for generating multiple related file names or creating ranges of numbers and letters, including features like zero-padding for consistent formatting. Additionally, command substitution enables the output of one command, such as the current date, to be inserted directly into another command's arguments, allowing for dynamic file naming based on real-time data.
The final sections of the video delve into arithmetic and parameter expansions, which are particularly useful in shell scripting contexts. Arithmetic expansion permits mathematical calculations within commands using integer-only operations, supporting standard operators like addition, subtraction, multiplication, division, and modulo for finding remainders. Parameter expansion allows access to stored data items known as variables by prefixing them with a dollar sign, while also clarifying that non-existent variables result in an empty string rather than causing an error. The video concludes by emphasizing that all these expansions occur sequentially before the actual command is executed, setting the stage for future lessons on controlling these behaviors through quotation marks.
Read the full video transcript
Let's start with a quick review of the
general model for using the shell.
The first item on an input line is a
command
followed by one or more options if any
and one or more arguments if any.
These parts are separated by spaces.
In this video, we're going to go more in
depth with how the shell processes your
input.
First, the shell uses blanks as a
delimiter, and extra blanks are ignored.
Here, I'm going to use the echo command
with lots of spaces between the
arguments,
and the command output has only one
space between each of the arguments.
One of the things the shell does with
your input is called pathname expansion.
And we've seen this throughout all of
our preceding videos.
One of the things a shell does with your
input is called pathname expansion.
When the shell encounters wildcard
characters,
it evaluates the wild card and replaces
it with the matching files.
This happens before the command is
executed.
This works for all wild cards. So if I
were to echo any arbitrary character
followed by a digit
followed by txt.
This would be my result.
If a wild card doesn't match anything,
it remains unexpanded.
Next up on the list is tilda expansion.
If you use a tilda all by itself, it
expands to the name of your home
directory.
If the tilda precedes a user's name,
it expands to that user's home directory
name.
If there's no such user,
then the tilda doesn't expand.
The next expansion is brace expansion
which makes it convenient to create
multiple related file names. If you
wanted to create this list of files, you
could use a brace expansion
which consists of an optional preamble,
a brace expression which can be a list
of items in braces separated by commas
and an optional postcript.
And here it is in action.
We'll have level dash and in braces a
comma b comma c with no spaces after the
commas
and then.info.
You can have multiple sets of braces.
For example, if I were to echo level
dash and then my first brace expression
would be ab c
and my second brace expression
would be 1 dot do three to indicate a
range of numbers or letters.info
it would generate a1 a2 a3 b1 b2 b3 and
c1 c2 c3.
When you have a range of numbers, you
can do zero padding. I'm going to make a
whole bunch of directories here. For the
next three years, I want to get one
directory per month. My first brace
expression will be 2026.2028,
then a dash,
and then my second brace expression will
be 01
to 12.
And if I do a listing of what's in my
directory now,
it's created all of those subdirectories
with a leading zero on the month numbers
that are less than 10.
One of the more complex expansions that
the shell does is called command
substitution.
Here's a command that shows today's date
in year, month, day format.
Let's say I wanted to create a file that
has today's date in it.
I'm going to create a file called notes
dash. And then I'm going to insert
today's date by using a dollar sign and
the command inside parentheses.txt.
And if I list my files now,
there I have notes-20269.txt.
And I can run this same command here
every day and get a file name for that
specific day.
The other expansions I want to cover are
mostly used in shell scripting.
Arithmetic expansion lets you do
calculations as part of a command.
Again, here I'm going to use echo. You
use a dollar sign followed by an
expression inside double parentheses. So
if I want 6 + 5 * 4,
that gives me 26.
An arithmetic expression can contain
additional parentheses if you need to
specify order of operations.
For example, here I'm going to evaluate
an arithmetic expression 4 * 6 + 5 and
that's in parentheses because I want the
addition to happen first and then
subtract two
and the answer is 42.
Arithmetic expansion is integer only.
So if I say 50 / 6 is and then my
expression is going to be 50.
By the way, you do not need to put
spaces around the arithmetic operators.
I left them out here, but normally I put
them in because it makes the expression
more readable.
So 50 / 6 is 8 because again it's
integer division.
If I need to find the remainder, I can
use the modulo operator which is a
percent sign.
So let me add to that previous command
with remainder.
And here my other arithmetic expression
is going to be 50 mod 6
and 50 / 6 is eight with remainder two.
Finally, there's parameter expansion.
The shell keeps data items in variables.
For example, the user variable contains
the current username.
In order to access the contents of the
variable, I need to proceed it with a
dollar sign.
So let me edit this command to say the
current username is dollar sign user
period at the end of the sentence
and it gets replaced with the content of
that variable.
If a variable does not exist such as
this misspelling
the shell will still expand it but the
result is an empty string.
You can see a list of all the variables
in the shell environment with the print
end command. And I'm going to definitely
pipe this to less because there are a
lot of variables
as you can see by paging through here.
In summary, here are the kinds of
expansion the shell does for us.
Pathname expansion. It expands wild
cards to the matching file names.
Tilda expansion lets us specify our home
directory or the home directory of
another user.
Brace expansion creates multiple text
strings from a pattern.
Command substitution lets us put the
output from one command into another
command.
Arithmetic expansion lets us use the
result of calculations as part of a
command.
And parameter expansion lets us access
shell variables.
And remember, the shell does all of
these expansions before it executes the
command.
In the next video, we'll learn how to
have some control over these expansions
through the use of quote marks.