Latest / CyberSecurity Summary / bash Cookbook: Solutions and Examples for bash Users 2nd Edition
Transcript
- 0:00Speaker 1: Imagine having a conversation where like, every time you pause
- 0:03Speaker 1: to take a breath, the person you're talking to completely
- 0:06Speaker 1: deletes your formatting.
- 0:08Speaker 2: Oh yeah, and just ignores half of what you actually meant, right, and.
- 0:11Speaker 1: Then mangles your sentence into something totally unrecognizable. I mean,
- 0:15Speaker 1: if you've ever typed a command into a computer terminal
- 0:17Speaker 1: and watched it instantly fail, you know exactly what I mean.
- 0:21Speaker 2: It's so frustrating.
- 0:23Speaker 1: It really is, because we tend to think of our
- 0:25Speaker 1: computers as these highly intuitive machines that you just want
- 0:29Speaker 1: to help us out. But when you strip away the
- 0:31Speaker 1: graphical interface, what you're left with is a blinking cursor
- 0:35Speaker 1: that is ruthlessly.
- 0:37Speaker 2: Literal, completely literal, and it's notoriously aggressive about how it
- 0:41Speaker 2: interprets your words. To a lot of people see that
- 0:43Speaker 2: blinking cursor the command line, and they just assume it's
- 0:47Speaker 2: a relic.
- 0:47Speaker 1: Like some archaic leftover from the nineteen.
- 0:49Speaker 2: Seventies, exactly like something we only keep around for it technicians.
- 0:53Speaker 2: But the command line is not a museum piece. It's
- 0:56Speaker 2: a programmable, highly efficient conversation with your operating system.
- 1:01Speaker 1: Which brings us to the core of our deep dive today.
- 1:04Speaker 1: I'm your host, and today we're pilling some incredible insights
- 1:07Speaker 1: from the Bash Cookbook, second edition by Carl Albing and
- 1:10Speaker 1: JP Vawson.
- 1:11Speaker 2: It's a fantastic resource.
- 1:12Speaker 1: It really is. And our mission today is to crack
- 1:15Speaker 1: open that dreaded cryptic command line interface. We're going to
- 1:19Speaker 1: decode the strange punctuation, learn how to pipe data around
- 1:24Speaker 1: like a well, like a master plumber.
- 1:26Speaker 2: Really, I love that analogy.
- 1:27Speaker 1: Thanks, and we'll discover why this text based tool is
- 1:31Speaker 1: still the absolute fastest shortcut to getting complex work done.
- 1:35Speaker 2: Because once you understand the grammar of this specific conversation,
- 1:38Speaker 2: it changes everything. It remembers your commands, lets you edit
- 1:41Speaker 2: them on the fly, and honestly, it can automate literally
- 1:44Speaker 2: any repetitive task you throw at it.
- 1:46Speaker 1: But before we start typing commands, we kind of need
- 1:48Speaker 1: to address what this black box actually is, because the
- 1:51Speaker 1: command line, or the shell as it's called, isn't actually
- 1:55Speaker 1: the operating system itself, which I have to admit blew
- 1:59Speaker 1: my mind a bit. I always assume the terminal window
- 2:01Speaker 1: was the core of the computer.
- 2:03Speaker 2: Oh, that's a really common misconception. But the separation between
- 2:06Speaker 2: the two is actually a foundational part of the original
- 2:09Speaker 2: Unix philosophy. How so well, in the earliest operating systems,
- 2:14Speaker 2: the user interface, the part that actually accepted your typed commands,
- 2:18Speaker 2: was hardwired straight into the operating system's core brain.
- 2:21Speaker 1: So they were bundled together, right, So if.
- 2:24Speaker 2: You wanted a better way to type commands for just
- 2:26Speaker 2: a new feature, you had to rewrite the entire operating
- 2:29Speaker 2: system to get.
- 2:30Speaker 1: It, which sounds a bit like trying to upgrade your
- 2:32Speaker 1: car's spearing wheel by replacing the entire engine.
- 2:35Speaker 2: That is a perfect analogy. It was incredibly inefficient, so
- 2:38Speaker 2: the creators of Unix separated them entirely. The operating system
- 2:42Speaker 2: handles the hardware, the memory, the heavy lifting, the engine
- 2:45Speaker 2: exactly the engine, and the shell is just a separate,
- 2:48Speaker 2: replaceable dashboard. Its only job is to listen to you
- 2:51Speaker 2: interpret your command and then pass it down to the engine.
- 2:55Speaker 2: And because it was a separate program, developers could write new,
- 2:58Speaker 2: better shells without ever touching the core system.
- 3:01Speaker 1: So you really could just swap out the dashboard whenever
- 3:03Speaker 1: you want. You could use shells with names like Crow
- 3:05Speaker 1: or KSh or Soush yep.
- 3:07Speaker 2: Absolutely, but today.
- 3:08Speaker 1: Our sources are focused entirely on Bash So why did
- 3:12Speaker 1: Bash become, you know, the universal rental car that everyone
- 3:15Speaker 1: knows how to drive.
- 3:16Speaker 2: It really comes down to a perfect storm of timing
- 3:19Speaker 2: and utility. Bash stands for the GNU born again.
- 3:23Speaker 1: Shell right, great pun by the way, Oh.
- 3:25Speaker 2: Programmers love their puns. But back in the late nineteen eighties,
- 3:29Speaker 2: the Gene project was trying to create a free, complete
- 3:32Speaker 2: operating system that followed PO six standards and posx.
- 3:36Speaker 1: Is essentially the universal rule book right for how Unix
- 3:39Speaker 1: systems should behave so software can run anywhere exactly.
- 3:43Speaker 2: They needed a standard shell for this free system, so
- 3:45Speaker 2: they built Bash by taking the heavy duty programming features
- 3:49Speaker 2: that developers required and combining them with the interactive conveniences
- 3:53Speaker 2: that everyday users wanted.
- 3:54Speaker 1: Things like just hitting the up arrow to see your
- 3:56Speaker 1: last command.
- 3:57Speaker 2: Yes, small things that made a huge difference.
- 4:00Speaker 1: Well, it clearly won the war. I mean, it's the
- 4:02Speaker 1: default on almost all Linux distributions historically, the default on
- 4:06Speaker 1: mac os, and you can even install it on Windows
- 4:09Speaker 1: through the Windows subsystem. For a Linux, it's everywhere. So
- 4:12Speaker 1: you learn Bash and suddenly you have a passport to
- 4:15Speaker 1: talk to almost any machine on the planet.
- 4:17Speaker 2: You do, but to speak the language you first have
- 4:20Speaker 2: to read the room. When you open that terminal, you're
- 4:23Speaker 2: greeted by a prompt, and that prompt usually ends with
- 4:27Speaker 2: a specific symbol, often a dollar sign or a hash symbol. Right,
- 4:30Speaker 2: and that tiny character is actually screaming a warning at
- 4:34Speaker 2: you if you know how to listen.
- 4:35Speaker 1: So let's decode that for the listener. If my prompt
- 4:38Speaker 1: ends in a dollar sign, the shell is basically saying
- 4:41Speaker 1: you are a regular user, you have normal safe permissions exactly.
- 4:45Speaker 1: But if it ends in a hash sign like the
- 4:46Speaker 1: number symbol, it's saying you are.
- 4:48Speaker 2: Root, the all powerful system administrator. As route, there are
- 4:52Speaker 2: no safety rails whatsoever. You can do.
- 4:54Speaker 1: Anything, which also means you can accidentally delete the entire
- 4:57Speaker 1: hard drive with a single typo.
- 4:58Speaker 2: Yep, seeing that hash sign is your visual queue to
- 5:01Speaker 2: double check every single keystroke.
- 5:03Speaker 1: Okay, So the prompt tells us who we are. It
- 5:06Speaker 1: also usually tries and tell us where we are in
- 5:08Speaker 1: the computer's folder structure. But the cookbook points out something
- 5:11Speaker 1: incredibly sneaky.
- 5:13Speaker 2: Here, oh about the paths.
- 5:14Speaker 1: Yeah, the prompt sometimes lies to you, or at least
- 5:18Speaker 1: it gives you an illusion like, wait, why would my
- 5:21Speaker 1: computer lie to me about what? Folder I'm in.
- 5:23Speaker 2: Well, it's not so much lying as it is keeping
- 5:25Speaker 2: up a facade that you created. Okay, fair, Think about
- 5:28Speaker 2: shortcuts on your desktop. In the Unix world, these are
- 5:31Speaker 2: called symbolic links. You might have a folder on your
- 5:34Speaker 2: desktop called photos, but the actual files are stored deep
- 5:37Speaker 2: inside some external hard drive. If you navigate into that
- 5:40Speaker 2: desktop folder and use the PW beauty command print working directory,
- 5:46Speaker 2: the shell might just say you are on the desktop.
- 5:48Speaker 1: Because that's the path I took to get there.
- 5:50Speaker 2: Precisely, If you type pdbudd dash L, it gives you
- 5:54Speaker 2: your logical path that's where you think you are based
- 5:57Speaker 2: on the shortcuts you followed.
- 5:58Speaker 1: Got it.
- 5:59Speaker 2: But if if you need to know where those files
- 6:01Speaker 2: physically reside on the spinning disc, you use PWD dash
- 6:05Speaker 2: p P for physical Right. The dash P strips away
- 6:10Speaker 2: all the illusions and reveals the true physical path.
- 6:14Speaker 1: That distinction alone can save like hours of troubleshooting. Yeah,
- 6:17Speaker 1: but here is where navigating gets truly dangerous. We all
- 6:21Speaker 1: have hidden files on our computers right. Oh yes, and
- 6:24Speaker 1: in Unix they're called dot files because their names literally
- 6:27Speaker 1: just start with a period exactly. So if I want
- 6:29Speaker 1: to see them, my instinct as a user is to
- 6:31Speaker 1: use a wildcard. I know what asterisk means everything, so
- 6:35Speaker 1: I figured I type the list command ls followed by
- 6:38Speaker 1: dot asterisk. You have to see all my hidden dot
- 6:40Speaker 1: file which seems totally logical, right, But the cookbook says
- 6:44Speaker 1: this will cause a catastrophic wall of text. Why if
- 6:47Speaker 1: the asterisk means everything, shouldn't dot asterisk just list my
- 6:51Speaker 1: hidden files?
- 6:52Speaker 2: This is such a classic trap, and it introduces a
- 6:55Speaker 2: process called globbing, which is how the shell actually expands
- 6:58Speaker 2: wildcards wlobbing. Okay, so the asterisk does mean any characters
- 7:02Speaker 2: zero or more times. You're totally correct that dot asterisk
- 7:05Speaker 2: matches any file starting with a dot.
- 7:07Speaker 1: Okay, so what's the problem.
- 7:08Speaker 2: The danger lies in how the Unix filesystem is built.
- 7:11Speaker 2: In every single directory without exception, there are two invisible folders.
- 7:15Speaker 2: One is named with a single dot, which is a
- 7:18Speaker 2: shortcut representing the directory you are currently in, ah right,
- 7:21Speaker 2: and the other is named with two dots dot dot,
- 7:24Speaker 2: which is a shortcut representing the parent directory exactly one
- 7:28Speaker 2: level above.
- 7:28Speaker 1: Oh, I see where this is going.
- 7:29Speaker 2: Yeah, when you type l's dot asterisk, the shell aggressively
- 7:33Speaker 2: expands that wildcard before the l's command even runs. Oh no,
- 7:37Speaker 2: it matches your hidden files, sure, but it also matches
- 7:40Speaker 2: the single dot and the double dot. So the shell
- 7:42Speaker 2: tells the l's command, please list the contents of the
- 7:46Speaker 2: hidden files, and the contents of the current folder, and
- 7:48Speaker 2: the entire contents of the folder above this.
- 7:51Speaker 1: One, which could be my entire home direction exactly. It
- 7:53Speaker 1: would just dump thousands of files onto my screen endlessly.
- 7:57Speaker 1: So how do we actually isolate the hidden files without
- 8:00Speaker 1: triggering an absolute avalanche?
- 8:02Speaker 2: The cookbook provides a brilliantly precise piece of syntax.
- 8:05Speaker 1: Or this.
- 8:06Speaker 2: You type dot, dot, bracket, exclamation point, dot, close bracket
- 8:11Speaker 2: as risk.
- 8:11Speaker 1: Okay, let me translate that out of alphabet soup a dot,
- 8:15Speaker 1: then an open bracket, an exclamation point, another dot, a
- 8:18Speaker 1: closed bracket, and finally an asterisk. What is the logic
- 8:22Speaker 1: behind that?
- 8:23Speaker 2: So brackets let you define a specific pool of allowed characters.
- 8:27Speaker 2: But when you put an exclamation point right inside the
- 8:30Speaker 2: opening bracket, it acts as a negator, like saying not
- 8:33Speaker 2: this exactly. It means absolutely not this character. So that
- 8:37Speaker 2: whole sequence translates to find a file that starts with
- 8:40Speaker 2: a dot. Now make sure the very next character is
- 8:43Speaker 2: absolutely not a dot. After that, the asterisk says you
- 8:46Speaker 2: can grab whatever is left. It perfectly targets your hidden
- 8:50Speaker 2: files while explicitly ignoring the single and double dot directories.
- 8:54Speaker 1: That is surgically precise, okay, and it brings us to
- 8:57Speaker 1: a massive realization about Bash. How you format your text
- 9:01Speaker 1: matters just as much as the command itself. Oh completely,
- 9:03Speaker 1: Because that shell is an aggressive listener. It intercepts parses
- 9:07Speaker 1: and modifies your words before it ever passes them to
- 9:10Speaker 1: the actual program you're trying to run.
- 9:11Speaker 2: That aggressive listening is exactly why spacing can drive new
- 9:15Speaker 2: users crazy. The shell uses spaces purely to figure out
- 9:18Speaker 2: where one argument ends and another begins.
- 9:20Speaker 1: Right, So, if I use the echo command, which just
- 9:23Speaker 1: prints whatever you type back to the screen, A type echo,
- 9:26Speaker 1: then the word this, Then I hit the space bar
- 9:28Speaker 1: ten times, then the word was, ten more spaces, and
- 9:31Speaker 1: the word spaced. What comes out on my screen is
- 9:34Speaker 1: just this was spaced with single spaces.
- 9:36Speaker 2: It looks completely normal.
- 9:38Speaker 1: Yeah, the shell just ate all my custom formatting.
- 9:40Speaker 2: Because it didn't see ten spaces. It just saw boundaries.
- 9:43Speaker 2: It grabbed the word this, the word was, and the
- 9:45Speaker 2: word spaced, packaged them up and handed those three neat
- 9:49Speaker 2: little boxes to the echo command. It threw all the
- 9:51Speaker 2: extra white space straight in the trash.
- 9:53Speaker 1: So if you want to protect your formatting from the
- 9:55Speaker 1: shell's aggressive interpretation, you have to lock it away using quotes.
- 9:59Speaker 2: Yes, quoting is essential, and.
- 10:01Speaker 1: Here we enter the absolute tyranny of quoting in Bash
- 10:05Speaker 1: because there is a massive fundamental difference between single quotes
- 10:09Speaker 1: and double quotes. I like to think of single quotes
- 10:12Speaker 1: like sealing your text inside a welded steel lock box.
- 10:16Speaker 2: That's a good way to look at it, right.
- 10:17Speaker 1: The shell physically cannot look inside. It just hands the
- 10:20Speaker 1: heavy box directly to the program. But double quotes are
- 10:24Speaker 1: like putting your text in a clear plastic bag. It
- 10:28Speaker 1: keeps the spaces together, but the shell can still peer
- 10:30Speaker 1: through the plastic and mess with the contents.
- 10:33Speaker 2: That is an excellent visualization. Double quotes protect your white space,
- 10:38Speaker 2: but they still allow the shell to perform substitutions. It'll
- 10:41Speaker 2: look inside that clear plastic bag for special characters like
- 10:45Speaker 2: dollar signs and exclamation points and actually try to execute them.
- 10:49Speaker 1: The cookbook has this hilarious example of how badly this
- 10:52Speaker 1: can go wrong. Yeah, imagine you want to print a simple,
- 10:55Speaker 1: frustrated sentence to the screen. A coffee is five dollars
- 11:00Speaker 1: with a dollar sign.
- 11:01Speaker 2: Very relatable script.
- 11:02Speaker 1: If you put that in double quotes, echo, double quote,
- 11:05Speaker 1: A coffee is dollar sign five, question mark, exclamation point,
- 11:09Speaker 1: double quote. It fails spectacularly.
- 11:11Speaker 2: It fails on two separate fronts. Actually, first, the shell
- 11:14Speaker 2: looks through the double quotes and spots the dollar sign
- 11:17Speaker 2: in bash. A dollar sign means you were calling a variable,
- 11:20Speaker 2: so it looks for a variable named five. Unless you
- 11:23Speaker 2: specifically created one. That variable doesn't exist, So the shell
- 11:27Speaker 2: just replaces dollar sign five with nothing.
- 11:29Speaker 1: It just vanishes Rea coffee.
- 11:31Speaker 2: Poof exactly, but the exclamation point makes it so much worse.
- 11:35Speaker 2: An exclamation point triggers history substitution.
- 11:38Speaker 1: Wait history, Yeah.
- 11:40Speaker 2: The shell tries to look back in your command history
- 11:43Speaker 2: for a previous command starting with whatever follows the exclamation mark.
- 11:48Speaker 2: It attempts to pull that old command right into your
- 11:50Speaker 2: current sentence, fails to find a match, and throws a
- 11:54Speaker 2: confusing event not found error. Oh man, your script just
- 11:57Speaker 2: completely crashes.
- 11:59Speaker 1: So learning to quote properly is literally the difference between
- 12:03Speaker 1: a working script and a script that makes you want
- 12:05Speaker 1: to throw your laptop out.
- 12:06Speaker 2: A window without a doubt.
- 12:07Speaker 1: If you just wrap that exact same sentence in single
- 12:10Speaker 1: quote steel lock box quotes, the shell wouldn't look for
- 12:13Speaker 1: variables or history. It would just print the sentence.
- 12:16Speaker 2: Understanding who is intercepting your commands is half the battle here,
- 12:19Speaker 2: and that also applies to the commands themselves. What do
- 12:22Speaker 2: you mean, Well, sometimes you type a command and you
- 12:24Speaker 2: don't actually know if you're talking to a program installed
- 12:26Speaker 2: in your hard drive or a feature built directly into
- 12:29Speaker 2: the Bash shell itself.
- 12:30Speaker 1: Ah like the difference between an external executable and a
- 12:33Speaker 1: built in exactly. For instance, the CD command to change directories,
- 12:36Speaker 1: that's a built in part of Bash, but a text
- 12:39Speaker 1: processing tool like AC is an external program. So how
- 12:43Speaker 1: do we reveal the truth about a command?
- 12:46Speaker 2: You ask the shell directly using the type or which commands? Okay,
- 12:50Speaker 2: if you type type CD, the shell will explicitly reply
- 12:53Speaker 2: CD is a shell built in. But if you type
- 12:56Speaker 2: type OC, it will give you the physical filepath on
- 12:59Speaker 2: your hard drive, pointing to the OUC program.
- 13:01Speaker 1: Why does that matter so much?
- 13:03Speaker 2: This becomes crucial when writing scripts for other people. Built
- 13:06Speaker 2: ins will always be there, but external programs might not
- 13:10Speaker 2: be installed on your coworker's machine.
- 13:12Speaker 1: Oh that makes total sense. Okay, So we've covered navigating
- 13:15Speaker 1: the system and formatting our commands so the shell doesn't
- 13:18Speaker 1: butcher them. Now let's talk about.
- 13:20Speaker 2: The results the output.
- 13:21Speaker 1: Right, we've essentially built a megaphone for our programs, but
- 13:24Speaker 1: where do they shout their answers. This brings us to
- 13:28Speaker 1: the fundamental magic of the Unix operating system IO redirection.
- 13:32Speaker 2: Input output redirection. In Unix, everything looks like a file.
- 13:37Speaker 2: A text document is a file, but your keyboard is
- 13:41Speaker 2: also treated as a file. Your screen is treated as
- 13:44Speaker 2: a file.
- 13:45Speaker 1: Which is such a brilliant abstraction. I don't need to
- 13:47Speaker 1: know how to write complex code to display pixels on
- 13:50Speaker 1: a monitor, or write drivers to interface with a USB
- 13:53Speaker 1: flash drive, not at all. The shell handles all that
- 13:56Speaker 1: translation using the greater than symbol the right pointing bracket.
- 14:00Speaker 2: Exactly, if you type echo hello greater than output dot txt,
- 14:03Speaker 2: the shell intercepts the output before it ever reaches your
- 14:06Speaker 2: screen and channels it directly into that text file instead.
- 14:10Speaker 1: But if I run that exact command again, it wipes
- 14:12Speaker 1: out whatever was already in that file and replaces it.
- 14:14Speaker 2: Rate it does it overwrites it completely.
- 14:16Speaker 1: So to safely add to the end of the file,
- 14:18Speaker 1: I have to use two brackets greater than greater than,
- 14:21Speaker 1: which tells the shell to append the data.
- 14:23Speaker 2: Appending is safer, yes, but if you want a true
- 14:26Speaker 2: safety net, Bash actually offers a brilliant feature called no.
- 14:30Speaker 1: Clobber no clawber. Great name it is.
- 14:33Speaker 2: By typing set dash no clawber, you instruct the shell
- 14:37Speaker 2: to aggressively protect existing files. If you accidentally use a
- 14:41Speaker 2: single grater then and point it out a file that
- 14:43Speaker 2: already has data in it, the shell will refuse to
- 14:46Speaker 2: overwrite it and throw an error instead.
- 14:48Speaker 1: No clawber is amazing. It really saves you from yourself.
- 14:51Speaker 1: But what if I really want to overwrite the file
- 14:54Speaker 1: like I've got no clawber turned on, but I know
- 14:56Speaker 1: what I'm doing and this specific file needs to be
- 14:59Speaker 1: wiped clean.
- 15:00Speaker 2: When you use the do it anyway operator the overrite right,
- 15:02Speaker 2: you type the greater than symbol followed immediately by a
- 15:05Speaker 2: vertical bar. That syntax tells the show I know the
- 15:08Speaker 2: safety is on, but bypass no clubber for just this
- 15:11Speaker 2: one specific command.
- 15:13Speaker 1: It's the manual override switch. Now, speaking of redirection, there
- 15:16Speaker 1: is a quicky behavior with the eel's command. That always
- 15:18Speaker 1: confused me.
- 15:19Speaker 2: Oh the column formatting.
- 15:20Speaker 1: Yes, when I type l's in my terminal, it formats
- 15:24Speaker 1: the files into a beautiful, neat grid across my screen.
- 15:28Speaker 1: But the second I redirect it to a file, l's
- 15:31Speaker 1: greater than save dot out and then open that file,
- 15:34Speaker 1: the grid is gone. It's just one long, single column
- 15:38Speaker 1: of file names.
- 15:40Speaker 2: Why does the formating completely change just because I send
- 15:43Speaker 2: it to a file.
- 15:44Speaker 1: That's actually by design, and it shows how smart these
- 15:46Speaker 1: tools are. Programs can actually detect the environment they're talking to. Really, Yeah,
- 15:50Speaker 1: The cell's command checks to see if its output is
- 15:53Speaker 1: going to a screen, a terminal, or if it is
- 15:55Speaker 1: being redirected into a file. If it detects a screen,
- 15:58Speaker 1: it assumes a human is reading it, so it formats
- 16:01Speaker 1: it into columns to save visual space.
- 16:03Speaker 2: Makes sense.
- 16:04Speaker 1: But if it detects a file, it assumes another computer
- 16:06Speaker 1: program might need to read that list later, and programs
- 16:09Speaker 1: strongly prefer reading data one line at a time, so
- 16:12Speaker 1: it automatically switches to a single column format.
- 16:15Speaker 2: It literally changes its behavior based on the audience.
- 16:18Speaker 1: That leads perfectly into a classic frustration every learner faces
- 16:21Speaker 1: with redirection. I remember I tried to redirect my compiler
- 16:25Speaker 1: errors to a file. I type my compile command then
- 16:28Speaker 1: greater than errors dot txt.
- 16:30Speaker 2: Let me guess yeap.
- 16:32Speaker 1: When I hit enter, the error is still spammed all
- 16:34Speaker 1: over my screen, and when I checked errors dot txt
- 16:38Speaker 1: it was completely empty. Why did the shell catch the errors?
- 16:42Speaker 2: This requires looking under the hood at file descriptors. When
- 16:46Speaker 2: a program runs in Unix, it opens three invisible channels
- 16:49Speaker 2: for data to flow through. They are numbered zero, one,
- 16:51Speaker 2: and two.
- 16:52Speaker 1: The channels zero, one and two okay.
- 16:54Speaker 2: Channel zero is standard input where the program gets its data,
- 16:57Speaker 2: typically your keyboard. Channel one is standard output. This is
- 17:00Speaker 2: the normal expected output of a program right and it
- 17:04Speaker 2: is buffered, meaning it saves up chunks of text and
- 17:06Speaker 2: sends them all at once for efficiency and channel two.
- 17:09Speaker 2: Channel two is standard error. This is a completely separate
- 17:12Speaker 2: pipeline dedicated exclusively to error messages, and crucially, it is unbuffered.
- 17:17Speaker 1: Meaning it doesn't wait exactly.
- 17:19Speaker 2: It spits text out instantly, character by character. That way,
- 17:23Speaker 2: if the program crashes dramatically and takes the system down
- 17:26Speaker 2: with it, the error message still makes it to your
- 17:28Speaker 2: screen before the lights go out.
- 17:30Speaker 1: So if channel one is standardshipping via the loading dock.
- 17:33Speaker 1: Channel two is like an emergency red phone line that
- 17:36Speaker 1: bypasses the loading dock entirely, so the error gets out instantly.
- 17:40Speaker 2: That's a great way to put it. When you used
- 17:42Speaker 2: a simple greater than symbol, you are only redirecting channel one.
- 17:46Speaker 2: Oh right, A single grater then is just shorthand for
- 17:49Speaker 2: one greater. Then you successfully redirected the normal output to
- 17:53Speaker 2: your file which was empty because your compiler failed. But
- 17:56Speaker 2: the errors were traveling down channel two, the red phone line,
- 17:59Speaker 2: which was still point it's straight at your screen.
- 18:01Speaker 1: So how do I catch channel two?
- 18:02Speaker 2: If you just want the errors, you pipe two greater
- 18:05Speaker 2: than errors dot txt, but usually you want both to
- 18:09Speaker 2: catch normal output A and D errors in the same file.
- 18:12Speaker 2: You'll often see this classic slightly cryptic syntax greater than
- 18:16Speaker 2: errors dot txt two greater than ampersan one.
- 18:19Speaker 1: Okay, let's break that down because it looks like a
- 18:21Speaker 1: typo to me. Greater than errors dot txt space two
- 18:26Speaker 1: greater than ampersand one.
- 18:28Speaker 2: Think of it like aiming two water hoses. The first part,
- 18:32Speaker 2: greater than errors dot txt points hose one standard output
- 18:36Speaker 2: into a bucket called errors dot txt.
- 18:39Speaker 1: Okay, hose one is in the bucket.
- 18:41Speaker 2: The second part two greater than ampersan one grabs hose
- 18:44Speaker 2: to standard error the ampersand is the key. Here it
- 18:47Speaker 2: acts as a reference pointer. It tells theshall, don't create
- 18:50Speaker 2: a new file named one. Instead, point hose two to
- 18:53Speaker 2: wherever hose one is currently aimed.
- 18:55Speaker 1: Oh, you're bundling the two hoses together into the same bucket.
- 18:59Speaker 2: Exact.
- 18:59Speaker 1: That makes so much much sense visually. Yeah. Now, if
- 19:01Speaker 1: we can seamlessly funnel output into a bucket, the next
- 19:04Speaker 1: logical leap is what if we funnel it straight into
- 19:07Speaker 1: another program? Oh? Yes, This is the digital plumbing of Bash.
- 19:10Speaker 2: The pipe, the pipe symbol. It's that vertical bar on
- 19:13Speaker 2: your keyboard, usually above the ender key. It is arguably
- 19:16Speaker 2: one of the most powerful concepts in all of computing.
- 19:19Speaker 2: It takes the standard output of the program on the
- 19:21Speaker 2: left and funnels it directly into the standard input of
- 19:24Speaker 2: the program on the right.
- 19:25Speaker 1: It's exactly like an assembly line conveyor belt. You have
- 19:29Speaker 1: one small machine that just finds files. It plops them
- 19:32Speaker 1: on the belt. The belt feeds them directly into a
- 19:35Speaker 1: machine that sorts them alphabetically. Right, that machine drops them
- 19:39Speaker 1: on a belt feeding into a machine that removes duplicates.
- 19:42Speaker 1: You're building a custom complex factory out of simple single
- 19:46Speaker 1: purpose tools.
- 19:47Speaker 2: M Because it's a pipe, it's actually performing rudimentary parallel processing.
- 19:52Speaker 2: The second program doesn't read for the first program to
- 19:54Speaker 2: finish completely. As soon as the first program produces a
- 19:57Speaker 2: single drop of data, it flows down the pie to
- 20:00Speaker 2: the next program.
- 20:01Speaker 1: But what if I'm building this complex pipeline and I
- 20:03Speaker 1: want to save a copy of the data halfway down
- 20:06Speaker 1: the assembly line without stopping the belt, Like I want
- 20:08Speaker 1: to sort the data, save a copy to check my work,
- 20:11Speaker 1: and then pass it to the duplicate remover.
- 20:13Speaker 2: Then you use a tool literally named after physical plumbing,
- 20:16Speaker 2: the T command tee. It acts exactly like a T
- 20:20Speaker 2: joint under your sink. You place it in your pipeline
- 20:22Speaker 2: for example, sort pipetcheck dot txt pipe unique. The T
- 20:27Speaker 2: command takes the flowing data, pours a copy into check
- 20:30Speaker 2: dot txt, and simultaneously lets the original stream continue down
- 20:34Speaker 2: the pipe so the next program can consume it.
- 20:36Speaker 1: It's perfect for debugging a long chain of commands. But
- 20:40Speaker 1: and there is a catch with pipes, right, Pipes only
- 20:42Speaker 1: pass data into a program standard input.
- 20:45Speaker 2: That's true, and some.
- 20:46Speaker 1: Programs stubbornly refuse to listen to standard input. They only
- 20:49Speaker 1: take arguments provide it directly on the command line the
- 20:52Speaker 1: ROM command. The remove command is a classic example. I
- 20:55Speaker 1: can't pipe a list of files into ARM, So how
- 20:57Speaker 1: do I use the output of a search command to deles.
- 21:00Speaker 2: Use command substitution instead of a pipe. You wrap your
- 21:03Speaker 2: search command in a dollar sign and parentheses like this
- 21:06Speaker 2: dot rm dollar sign open parenthesis, fine dot dash name
- 21:10Speaker 2: asterisk dot beck close parenthesis.
- 21:13Speaker 1: So dollar sign open parenthesis, the fine command close parenthesis.
- 21:16Speaker 1: How's that different from a pipe?
- 21:18Speaker 2: The shell sees that dollar sign parenthesis syntax, and it
- 21:22Speaker 2: essentially presses pause on your main command. It opens a
- 21:24Speaker 2: completely separate subshell runs the inner fine command, gathers all
- 21:28Speaker 2: the texts that command spits out, and literally paste that
- 21:31Speaker 2: text back onto the command line right after the ARM wow.
- 21:34Speaker 2: It substitutes the command with its own output, and then
- 21:37Speaker 2: presses play on the main ARM command.
- 21:39Speaker 1: That sounds incredibly powerful, but also incredibly dangerous.
- 21:44Speaker 2: Oh, it is the definition of dangerous. RAM is unforgiving.
- 21:48Speaker 2: There is no recycle bin on the command line. Right
- 21:51Speaker 2: if you make a typo in that inner fine command
- 21:53Speaker 2: and it locates more files than you intended. Say it
- 21:56Speaker 2: accidentally lists every file in your home directory. The subshell
- 22:00Speaker 2: will paste all of those file names directly into the
- 22:02Speaker 2: arm command and they are gone forever ouch.
- 22:05Speaker 1: So the rule is always test the inner command first.
- 22:08Speaker 1: Always run the fine command by itself. Verify the list
- 22:12Speaker 1: of files on your screen. If it is exactly what
- 22:14Speaker 1: you want to delete, then you can wrap it in
- 22:16Speaker 1: the dollar sign in parentheses and hand it to RAM.
- 22:18Speaker 2: Precision is everything.
- 22:20Speaker 1: Okay, so we've mastered building megaphones. We can pipe data out,
- 22:24Speaker 1: redirect it, and substitute it. Let's pivot. What if a
- 22:27Speaker 1: program needs us to feed it raw materials? First, how
- 22:30Speaker 1: do we force feed data into a system. Because this
- 22:33Speaker 1: is where shell scripts evolve from static lists of commands
- 22:37Speaker 1: into truly interactive tools.
- 22:39Speaker 2: Well, if output redirection uses the greater than signed to
- 22:41Speaker 2: push data out, input redirection uses the less then signed
- 22:45Speaker 2: to pull data in, it points the other way, right.
- 22:47Speaker 2: If a program expects you to manually type data on
- 22:50Speaker 2: the keyboard, you can just use less than data dot
- 22:53Speaker 2: txt to pour the contents of that file directly into
- 22:57Speaker 2: the program's input.
- 22:58Speaker 1: But what if I am writing a script to share
- 23:00Speaker 1: with my team, and I don't want to email them
- 23:02Speaker 1: the script file on a separate data file. I want
- 23:04Speaker 1: the data hard coded right inside the script itself.
- 23:07Speaker 2: You use what's called a here document. The syntax is
- 23:09Speaker 2: two less than signs followed by a marker word. Traditionally,
- 23:12Speaker 2: programmers use eof, which stands for end of file.
- 23:15Speaker 1: Wait, how does less than less than eof work? Conceptually?
- 23:19Speaker 2: Normally, a script asks for a file on your hard
- 23:22Speaker 2: drive to get data. The double less then tells the
- 23:25Speaker 2: shell don't go looking on the hard drive. The file
- 23:28Speaker 2: is right here glued to the bottom of this script.
- 23:31Speaker 2: Keep reading the following lines and treat them as raw
- 23:33Speaker 2: data until you hit this exact password eof on a
- 23:37Speaker 2: line by itself.
- 23:38Speaker 1: It's like an inline data stream. But the cookbook mentions
- 23:42Speaker 1: a bizarre bug that happens with here documents.
- 23:45Speaker 2: Oh, the dollar sign issue.
- 23:46Speaker 1: Yeah. Let's say I write a script to log charitable donations.
- 23:49Speaker 1: I use a here document to store the data. PETE
- 23:51Speaker 1: gave one hundred dollars with a dollar sign. But when
- 23:54Speaker 1: I run the script, it prints out PETE gave zero zero.
- 23:58Speaker 1: What did one hundred bucks go?
- 24:00Speaker 2: We are back to the aggressive listener and the tyranny
- 24:02Speaker 2: of the dollar sign Even inside a here document, the
- 24:05Speaker 2: shell is still scanning your text. No, it sees dollar
- 24:08Speaker 2: sign one hundred. It assumes dollar sign one is a
- 24:11Speaker 2: variable you are trying to call. That variable is empty,
- 24:14Speaker 2: so it deletes the dollar sign one, leaving just the
- 24:16Speaker 2: zero zero.
- 24:17Speaker 1: So how do we stop it from scanning the data.
- 24:19Speaker 2: We go back to our quoting rules. If you put
- 24:22Speaker 2: single quotes around your marker word typing less than quote
- 24:27Speaker 2: eof quote, you are essentially locking the entire here document
- 24:32Speaker 2: in that steelbox. You're telling the shell turn off all
- 24:35Speaker 2: special features. Do not expand variables. Just read this data
- 24:39Speaker 2: exactly is written. Your one hundred dollars remains one hundred dollars.
- 24:43Speaker 1: That is a subtle fix, but incredibly powerful. Though the
- 24:47Speaker 1: cookbook also notes the eof marker itself is incredibly.
- 24:50Speaker 2: Fragile, extremely fragile. The ending marker must be pristine. If
- 24:54Speaker 2: you have even a single trailing blank space after the
- 24:57Speaker 2: word eof at the end of your data, Bash will
- 25:00Speaker 2: recognize it.
- 25:00Speaker 1: It'll just keep going.
- 25:01Speaker 2: Yes, it will think the data is still going. It
- 25:03Speaker 2: will swallow the rest of your script, treating your actual
- 25:06Speaker 2: commands as text data, and the whole thing will silently.
- 25:08Speaker 1: Break No trailing space is allowed. Got it?
- 25:11Speaker 2: Now? What if we don't want a hard code data.
- 25:13Speaker 2: What if we want our script to actually pause and
- 25:16Speaker 2: ask the human a question in real time?
- 25:18Speaker 1: That is the job of the read command. It grabs
- 25:20Speaker 1: input directly from the keyboard and stores it in a variable,
- 25:23Speaker 1: and it has some fantastic flags for usability.
- 25:25Speaker 2: Like what You can use read dash P to provide
- 25:28Speaker 2: a custom prompt like read dash P enter your name.
- 25:32Speaker 1: And my absolute favorite feature is read dash T, which
- 25:35Speaker 1: sets a time limit. Yes, you could do read dash
- 25:37Speaker 1: T three dash pe answer quickly. If the user doesn't
- 25:40Speaker 1: type anything within three seconds, the read command times out
- 25:43Speaker 1: and the script just moves on to the next line.
- 25:45Speaker 2: It's brilliant for automation. It allows you to write scripts
- 25:49Speaker 2: that run on their own with default behaviors, but still
- 25:52Speaker 2: give you a three second window to interrupt them if
- 25:54Speaker 2: you happen to be watching the screen. It's the mark
- 25:57Speaker 2: of a truly professional script.
- 25:59Speaker 1: So we started this deep dive looking at a blinking cursor,
- 26:03Speaker 1: feeling completely intimidated by its aggressive, literal nature. But now
- 26:07Speaker 1: we see it for what it is. The command line
- 26:09Speaker 1: isn't a nostalgic textbox. It is the connective tissue of
- 26:12Speaker 1: the operating system.
- 26:13Speaker 2: It really is.
- 26:14Speaker 1: By understanding the rules of quoting, knowing how filed descriptors
- 26:17Speaker 1: route your data, and mastering the plumbing of pipes and redirects,
- 26:21Speaker 1: you stop just using your computer and you start commanding it.
- 26:24Speaker 2: And that is the true joy of Bash. It rewards
- 26:26Speaker 2: precision and it vastly amplifies your creative.
- 26:29Speaker 1: So we'll leave you with a challenge. Think about your
- 26:31Speaker 1: daily digital life. The command line is basically just a
- 26:34Speaker 1: highly efficient conversation using tiny, modular verbs. What complex, repetitive
- 26:40Speaker 1: task do you do every single week? Resizing images, organizing downloads,
- 26:44Speaker 1: searching for errors and logs that could be completely automated
- 26:47Speaker 1: with just three or four words piped together. The cursor
- 26:50Speaker 1: is blinking. It's waiting for you to start the conversation.