In this video, we'll tell about shell expansion.
So what is shell expansion?
Well, it's a generic feature that
allows for more efficient command line use.
It starts with globbing.
Globbing expands file names based on wildcards,
like ls star, where the star represents any character,
no matter how many characters.
You can also make it more complicated
and include question mark.
Question mark is one single character.
And you have ranges, like lsae star,
which is showing all files that have a name starting
with an A, B, C, D, or E.
Other types of expansion also exist,
like brace expansion, which means
that you can create an entire series of elements,
as in touch file curly brace 1.9, which will create file 1
up to file 9.
We have tilde expansion, cd tilde.
Tilde is your home directory.
It has command substitution, ls minus l on dollar, which ls.
Which ls is showing the current path name of the ls command.
And ls minus l on dollar, which ls
will work with the result of the which ls command.
This is particularly useful in scripts
where you want to run on the result of a command that
is not always producing the same result.
And then we have variable substitution,
where you are going to work on the result of a variable.
Let me show you all of these.
Let me get into the etc directory,
where I'm using ls star, which is just showing
all items in the etc directory.
Now, ls in combination with the star wildcard
has weird behavior.
If it finds a directory that matches the pattern,
it will also show you the contents of that directory.
Honestly, I don't like this very much.
And that's why I'm making it ls minus d star.
Minus d means that it will show the names of matching
directories, but not the contents.
And what happens if you make that ls minus d a star?
Well, only names that are starting with an a.
And ls minus d a question mark a star,
well, we are looking for items that
have an a at the first position.
Then the second character, we don't care.
And the third character, we need an a as well.
And finally, ls minus d on a dash e star
is showing everything that has a name starting
with an a, b, c, d, or e.
For the next demo, I need to go to my home directory.
cd, without anything behind it, brings you
to your home directory.
And there, I can use touch, file curly brace 1.9.
So the touch command creates an empty file
with a name that's specified.
And here, I don't want to create one empty file.
I want to create a couple of them.
So when I use ls, do you see that?
We have file 1 up to file 9.
And I can do the opposite as well.
If I want to do some housekeeping,
rm on file 1.9 is removing all the files that I just created.
Now tilde expansion, cd tilde is bringing you
to your home directory.
And then we have command substitution.
One command that I like a lot is uname minus r,
which is giving my current kernel version.
And I can do something, because there is this directory.
So how can we use it?
Well, I know that in the directory lib modules,
there's my kernel modules, which are basically drivers.
And there's a directory for every current version
of the kernel.
Well, uname minus r happens to be exactly the name
of that directory.
So if I want to see what is in the directory that corresponds
to the name of my current kernel,
I can use ls minus l slash lib slash modules slash,
and here it goes, dollar between parentheses uname minus r.
And there we can see the contents
of this exact directory.
And why is this cool?
Well, this is cool, because kernel versions typically
change a lot.
Final thing is variable expansion.
So when I use echo dollar path, I
can see the result, the contents of my path variable.
Variables are pretty useful, and you will encounter them
a lot as well.