Shell – 5 – Bash Shell Expansions
Video Title: Shell – 5 – Bash Shell Expansions
Let’s talk about shell expansions which is a concept that is prevalent in working at a bash shell but is not very familiar if you’re coming from a general-purpose programming language so the idea is we’re going to be able to give a command such as echo and then maybe a single word like Hello
And then maybe a string that has a variable name access in it that would be let’s say name right and the idea of shell expansion is this right so when the command is given the first thing that’s going to happen is the interpreter is going to break this into
Tokens right so this would be a token this would be a token and this whole string would wind up being a token so we’ve got a double quoted string value that would be a single token even if and let’s actually make this a little bit more interesting even if I had some
Spaces here and maybe some exclamation points after that space all right so that would be a single token right and then what’s gonna happen is step number two is each token is expanded so the idea of expansion is here we have just a very basic ordinary string and nothing’s
Gonna happen it’s gonna be the same as what it was before same with Hello right just an ordinary string nothing is gonna be expanded this string however has a what we call a parameter expansion in it and in where we can think this is a variable expansion so let’s imagine that on above
This in code we had previously given a command like a sine the variable name to be Chris right and so what happens during the variable expansion step is we would say okay let’s substitute or expand this special construct to be replaced with that variables contents and then whatever followed and then the
Last step are actually before we get to this command being interpreted the other part that happens here is the the quotes are going to be removed so these quotes that we had written out would should be removed but this would still be a single unit right so this would be
One unit hello would be another unit and so we’ve got three things that are being processed here or three strings that are being processed I could say echo hello and then this string value here but none of those would have quotes before they get passed into the actual command that
Interprets what this is right so when the echo program runs the contents of its first argument would be hello and second would be crisps followed by a space followed by exclamation points and those would be a string or textual values all right so the idea here is there are actually some other expansions
That we’ll learn about and what’s interesting is that these happen this expansion occurs before the program runs right so this echo program does not know anything about the idea of this this variable reference inside of this string value no it was the shells job to expand that string and carry out whatever
Substitution was needed in order for echo to then be given exact string values with those variables pre-populated right so this expansion occurs before any programs or functions or whatever it is that you’re using this expansion in it gets evaluated right so let’s take a look at some other types of
Expansions the first one which was alluded to before is filename globbing this is very very commonly used and powerful expansion will look at brace expansion and then command substitution there are of course many more details here that you can find and learn more about in the bash manual but these are I
Think the most important to have a sense of how they work so the first one is a filename globbing and the idea is when you have an uncoded path so in this case you don’t want to you can’t use filename globbing inside of a quoted string so
When you have an uncoded path you can use some special matching characters and the most commonly uses the asterisk which says hey match any number of characters but don’t match the directory set so for example here if we said echo slot slash bin slash and then star grep we
Would match any program whose name in the bin directory began with with any string of characters and then was ended or had a suffix of grep right additionally if we wanted to find names of characters sorry names of programs with three characters the question mark in filename glopping will expand to
Match any single character so we can try these examples out real quick so I’m going to say LS slash bin slash asterisk grep and noticed that there are a number of programs in the bin directory that begin with any string of characters and end with the word grep and so the
Asterisk is matching multiple characters there and the second example was LS slash bin and then any three letter command so notice that cat der PWD said and thar are all three letter program names in these what’s important to understand here is this will work with
Echo as well so alas I don’t want to confuse what’s happening here because the LS program might have been doing something more interesting but to prove to ourselves that remember echo all it does is it it it prints back out to standard output what we give it as the
Input argument so if I were to say slash bin question mark question mark question mark notice that we’re getting very similar output and here the list of arguments that echo received right so the list of things that echo received are these strings to hopefully make this even more evident in the previous video
We defined a function named print all right and if you wanted to go ahead and just read Eclair this function you could by writing it out all in one line and again we’re using the semicolon to to mark the end of statements so this print all function loops through each argument that is
Passed into this function as an argument race remember this is a special variable that says the list of all arguments and we’re going to echo each of those arguments on their own line right so now if I were to say print all and then slash bin and then question mark
Question mark question mark notice that for this to have worked that expansion what happened was at Shell expansion time there are two tokens here right so this is the first token and this is the second token and we expanded this it turns out there’s nothing needed to be
Expanded here there are no variable references or anything like that but this did include these file globbing characters so the three question marks or the asterisk also will result in this and that’s going to actually expand into more than one string so the single input string actually expanded into slash bin
Slash cat slash bin slash Durr and you get the point right so it’s as if we had written the command print all followed by all of the names of the files in that directory that matched this particular globbing expansion right so that’s a very powerful tool in conjunction with the ability to write
Loops and scripts and also the ability to write to give commands that operate on multiple files right so for example if I had wanted to use the cat program to read all of the markdown files in this directory out I could say cat star dot MD right into this globbing pattern
Would say expand into into what well it would expand into the word cat as the first value and then this would expand to demo dot MD followed by hello vem MD and it would be like us as the operators of this script or if we had this line in
Some shell script when we type this it’s as if we are typing this can and here these expansions are evaluated before this cat program will run and so when the cat program actually is run it will have these two inputs as a part of it
So when I do that we see that the output is there and that would have been the same output as if I had written cat demo dot MD and cat hello MMD great so globbing is a very powerful mechanism when you’re trying to write scripts that operate over a list of
Files that match some some pattern and often this will be files that are of a certain type like all my C files or all my markdown files things like that so as I just mentioned one pattern string expands two mini matched path streams so this is an example where you start with
One input and it actually the expansion results in multiple string inputs that are going to be passed off to the command that gets run in vim the next expansion I want to talk about our braced lists and this is a it has some relationship to the other to the
Previous example of clapping but the idea is when we have curly braces surrounding a list of comma separated strings that will expand to n separate strings so let’s actually rather than thinking of this formally we’ll come back to that let’s think of this as an example so if I echo one two three
You’ll notice that we have it’s as if echo was passed those three things individually or if I were to use the function we defined previously which was print alright so if I use the print all function one two three notice that expands to three different inputs right
Or three arguments now you might be thinking well what’s useful about that why would I ever use this well imagine that we have some longer path here and then there’s three files in that path that we want to address right when we use this expansion it takes
Whatever prefix and we could also have a suffix here if we wanted so here’s a suffix an example and we’re it find those braced words it’s going to take each of them in produce or expand to an individual string for each of those values right so here we see that some
Longer path one two three expands to these three values and there are some common use cases for this for example if I look in my say wiki directory and notice I have this file named index dot wiki let’s say I wanted to rename that file so I would say move wiki slash and
The current name is index dot wiki and let’s say that I wanted the new name to be say renamed demo right and then dot wiki is the suffix so when I carried out that command right this expanded to move right so move followed by there are two strings generated here so the first
String generated would have been wiki slash index dot wiki and notice that’s the name of the file that we started from so remember move is going to say take some current path and move it so that it gets the its path has moved to some other path right and so the other
Path that it gets moved to is wiki slash rename demo without these this curly brace expansion this would have been the complete command we needed to run right and there’s some redundancy here and so one use case of this curly brace expansion is a simple move another one
That’s commonly used is if you want to make a directory structure like say I wanted to make a directory structure for a new project and I wanted to say something like make dur and I’m gonna use the P flag here to make any parent directories necessary so we’ll recursively make
Directories here and let’s say a project demo slash and here I’ve got a source directory a bin directory and an include directory for example and that would have expanded to make durch P and then three separate arguments as input such that if I were to now look at well okay
What’s in my projects demo directory we created each of those three directories as their individual freestanding arguments because we used this expansion so this is a common cool expansion that you’ll use when working with the file system and there’s some shared path that you want to repeat some operation on
Just like we saw here the last thing I want to just demonstrate here which I’ve never had a real use for but just brings us conceptually full circle is if I use the print all again and I say like something like ABC and slash de f so
Notice we’re using two sets of curly braces this will go through and permute all expansions right of both combinations here and if you have a good use for this let me know I think it’s conceptually pretty cool but I’ve never actually needed to use that in a real
Life environment right so some of those examples that I mentioned before moving a file or renaming a file using some old name and new name such that you don’t have to re-type out the path that it is that is is really handy when you want to make sub directories of a project that’s
Also a commonly used use case for this bracelet expansion the last one I want to talk about in this video is command substitution so with command substitution one of the things we can do is actually run some other command and substitute its output wherever we ran that command in some other string right
So we can use this and that will see that the starch that dollar symbol just like a variable access but rather than curly braces we’re gonna use parentheses to say this is effectively a sub command or we’re using this as command substitution and whatever the output of this command is
It’s going to be substituted in where it was used okay so for example there’s a command that’s named Who am I that will tell us what the user name of our current user is okay so let’s go try out command substitution so there’s a command name date for example that
Prints out the current date of our system and if I were to say echo the current date is and then I’m going to use command substitution here to actually run that command in place of that whatever its output is gets substituted there and so you can see
That is what happened we can do things like echo or let’s do print all and then I can use command substitution for the output of LS and notice that all of the strings that were a part of this LS command were passed to our print all function as arguments and so this
Expanded to each of these we can try something like echo and then a command such as Who am I which would tell us the you current user of some system and we’re currently working in a docker session where were the root user which is like the super admin of a given
System and so that just told us that we were the root user and notice that we can run any command that we want here and its output whatever is sent to standard output gets substituted in that string
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
This has really inspired me, exciting stuff.
learning python at the same time as this. The echo command makes alot more sense with the help of this video because it reminds me of how in python you would have a set variable value and then you would use the print function or in this case echo and it would print the value of the variable rather than the name of the variable itself unless you wanted it to in which case you would leave it just naked in double quotes etc. im rambling lmao Thanks!!
4:01 – 9:57 file name globbing
9:58 – 15:12 braced lists
15:13 – command substitution
Thanks for sharing
I have a good use for the double braces expansion: Java projects built with Maven normally have the directory structure generated by the following command: mkdir -p src/{main,test}/{java,resources}. I learned this trick with this video, thanks!
Awesome teaching! Subbed.
Thanks for this video bro, helps beginners a lot
Awesome man
good tutorial , the examples you made help me a lot to understand this concept