Latest / CyberSecurity Summary / Bash Cookbook 1st Edition
Transcript
- 0:00Speaker 1: Imagine finding a hidden door in your house that you
- 0:03Speaker 1: never knew existed.
- 0:04Speaker 2: Like a secret passage or something exactly.
- 0:06Speaker 1: Yeah, you pry it open, you step inside, and you
- 0:09Speaker 1: realize it's an entire master control room.
- 0:11Speaker 2: Wow.
- 0:12Speaker 1: Right, and suddenly you can bypass every single like switch,
- 0:15Speaker 1: every thermostat, every lock in the building. You just have
- 0:18Speaker 1: this absolute, unfettered access to the underlying wiring of your home.
- 0:23Speaker 2: That's a powerful image and a little.
- 0:25Speaker 1: Intimidating it is, But I mean that is exactly what
- 0:28Speaker 1: happens when you open the command line on your computer.
- 0:31Speaker 1: So welcome to this custom tailored deep dive, designed specifically
- 0:36Speaker 1: for you.
- 0:36Speaker 2: Glad to be here for this one.
- 0:38Speaker 1: Yeah, Our mission today is to demystify this hidden world,
- 0:42Speaker 1: and we're using excerpts from the Bashed cookbook. So, whether
- 0:46Speaker 1: you're a seasoned programmer or you know, just insanely curious
- 0:49Speaker 1: about what happens behind that graphical curtain of your desktop,
- 0:52Speaker 1: our goal is to reveal how mastering this text based
- 0:56Speaker 1: layer is. Well, it's basically a shortcut to total control
- 1:00Speaker 1: over your machine.
- 1:00Speaker 2: Yeah, Because you know, most of us are just entirely
- 1:04Speaker 2: accustomed to a visual, white and click reality. We navigate
- 1:08Speaker 2: our digital lives the exact same way we navigate a
- 1:10Speaker 2: physical kitchen. Like, if you want to throw a file away,
- 1:14Speaker 2: you look for a little trash can icon.
- 1:15Speaker 1: Right to grab the file, drag it over done exactly.
- 1:18Speaker 2: It's spatial and it feels safe. But when you strip
- 1:22Speaker 2: away that graphical illusion, the trash can just vanishes, goof gone, right,
- 1:27Speaker 2: and you're left staring at a blank screen with just
- 1:30Speaker 2: a blinking cursor. I mean, it is an incredibly powerful
- 1:34Speaker 2: text based environment, but it's just sitting there waiting for
- 1:37Speaker 2: direct instructions.
- 1:38Speaker 1: Okay, let's unpack this because before we can start typing
- 1:41Speaker 1: commands into that blank screen, we kind of need to
- 1:44Speaker 1: understand what we're actually typing into. We use the word
- 1:47Speaker 1: shell all the time and computing, but to really grasp
- 1:50Speaker 1: why a shell even exists, we have to rewind all
- 1:54Speaker 1: the way to the nineteen seventies and look at the
- 1:57Speaker 1: creation of the Unix operating system.
- 1:59Speaker 2: Yeah, and if we connect this to the bigger picture,
- 2:01Speaker 2: early computing was highly highly monolithic.
- 2:04Speaker 1: What do you mean by monolithic?
- 2:06Speaker 2: Well, the user interface and the core operating system were
- 2:08Speaker 2: just welded together in one massive piece of software. Oh
- 2:11Speaker 2: I see, Yeah, you were completely stuck with whatever interface
- 2:14Speaker 2: the hardware vendor decided to provide. But the brilliant concept
- 2:19Speaker 2: behind Unix was this clean, deliberate separation. They split the
- 2:24Speaker 2: system into two distinct parts, the kernel and the shell.
- 2:28Speaker 2: Kernel in the shell, right, the kernel is the core.
- 2:31Speaker 2: It handles all the brutal heavy lifting of computing, like
- 2:35Speaker 2: managing memory allocations, talking directly to the physical hardware, scheduling
- 2:39Speaker 2: CPU time, and nuts and bolts. Exactly. It's all the
- 2:42Speaker 2: stuff the average user never ever wants to manage manually.
- 2:45Speaker 1: So if the kernel is essentially the car's engine and
- 2:49Speaker 1: the drive.
- 2:50Speaker 2: Train, right, that's a good way to look at it.
- 2:51Speaker 1: Then the shell, I guess, is the steering wheel, the pedals,
- 2:54Speaker 1: and the dashboard. It's the interface that lets the driver
- 2:57Speaker 1: actually tell the engine what to do.
- 2:58Speaker 2: Yes, the absolute genius of UNUS was realizing that you
- 3:03Speaker 2: could swap out the steering wheel without having to rebuild
- 3:06Speaker 2: the entire engine from scratch, which is huge. It is revolutionary.
- 3:10Speaker 2: The shell became just another swappable program.
- 3:12Speaker 1: And that decoupling must have allowed for like an absolute
- 3:15Speaker 1: explosion of innovation right.
- 3:17Speaker 2: Oh, completely, because if developers felt a command interface was inefficient,
- 3:22Speaker 2: they didn't have to go and convince a massive corporation
- 3:24Speaker 2: to update an entire operating system.
- 3:27Speaker 1: They could just build their own steering wheel exactly.
- 3:29Speaker 2: They can simply write a new shell, and they did.
- 3:31Speaker 2: I mean, the sources mentioned the original born shell, which
- 3:34Speaker 2: was known as SHE. Then the CShell Shish emerged. It
- 3:38Speaker 2: had some new user conveniences, but it had quirks that
- 3:41Speaker 2: made programming kind of difficult.
- 3:43Speaker 1: Gotcha.
- 3:44Speaker 2: Following that was the corn Shell Gish, And finally, the
- 3:49Speaker 2: undisputed star of our source material today, the Born Again
- 3:53Speaker 2: Shell or Bash bash.
- 3:55Speaker 1: Which is basically everywhere now. Like, if you open a
- 3:58Speaker 1: terminal on almost any line Nicks distribution or a Mac today,
- 4:02Speaker 1: you were likely looking at Bash.
- 4:04Speaker 2: Oh almost certainly.
- 4:05Speaker 1: And the sources point out it achieved this total dominance
- 4:08Speaker 1: largely due to the open source movement, right specifically the
- 4:11Speaker 1: Genu project. Like Brian Fox wrote Bash as a free
- 4:14Speaker 1: replacement for the original born Shell.
- 4:15Speaker 2: Yeah, that was a huge part of it.
- 4:17Speaker 1: But there's also this term mentioned in the text POSEX standardization.
- 4:21Speaker 1: What actually is PO six and why did it help
- 4:24Speaker 1: Bash take over the world?
- 4:26Speaker 2: Right? So PO six stands for Portable Operating System interface. Okay,
- 4:30Speaker 2: back in the day, different versions of Unix were fragmenting badly,
- 4:35Speaker 2: like a script written on one system might completely break
- 4:38Speaker 2: on another because the underlying commands behaved just slightly differently.
- 4:42Speaker 1: Oh, that sounds like a nightmare for developers.
- 4:44Speaker 2: It was so. POSX was created as a standardized set
- 4:48Speaker 2: of rules. It dictated exactly how an operating system and
- 4:51Speaker 2: its shell should behave and Bash was designed to be
- 4:54Speaker 2: highly POSEX compliant.
- 4:56Speaker 1: Ah, so it was like a guarantee exactly.
- 4:59Speaker 2: It meant to developers knew if they wrote a script
- 5:01Speaker 2: for Bash, it would run reliably across wildly different hardware systems.
- 5:05Speaker 2: It essentially became the universal translator of systems administration.
- 5:09Speaker 1: That makes total sense. So, okay, now that we know
- 5:11Speaker 1: we're sitting behind the swappable steering wheel called Bash, we
- 5:14Speaker 1: need to know how to.
- 5:15Speaker 2: Read the dashboard, right, the interface itself.
- 5:18Speaker 1: Yeah, we're staring at that blinking cursor and it's sitting
- 5:22Speaker 1: right next to something called the prompt. And according to
- 5:25Speaker 1: the cookbook, that prompt is packed with vital information about
- 5:28Speaker 1: who you are and what you can do.
- 5:30Speaker 2: It tells you everything you need to know about your
- 5:32Speaker 2: current state.
- 5:32Speaker 1: Right, and the very last character of the prompt is
- 5:36Speaker 1: the dead giveaway like a trailing dollar sign means you
- 5:39Speaker 1: are logged in as a regular user with standard safe permissions. Yes,
- 5:44Speaker 1: but if you see a hash sign, you know the pound.
- 5:47Speaker 2: Symbol ake, you need to be very very careful.
- 5:49Speaker 1: Why what does the hash mean?
- 5:51Speaker 2: It means you are operating as root, that is the
- 5:53Speaker 2: all powerful system administrator account. You have zero restrictions. You
- 5:58Speaker 2: can modify any file, read any data, and consequently you
- 6:03Speaker 2: can accidentally obliterate the entire operating system with a single typo.
- 6:07Speaker 1: Yikes. So the ash symbol is basically a glaring red
- 6:09Speaker 1: warning light that the safety rails are completely off.
- 6:12Speaker 2: Precisely, it's the shell saying I will do whatever you type,
- 6:15Speaker 2: even if it destroys everything.
- 6:17Speaker 1: Good to know. Okay, So, assuming we are safely using
- 6:20Speaker 1: our dollar sign prompt, we actually want to run a command.
- 6:22Speaker 1: The cookbook details several ways to find the tools we need.
- 6:25Speaker 2: Right, Finding the right command is half the battle.
- 6:27Speaker 1: Yeah. So there's type, which tells you if a command
- 6:31Speaker 1: is a built in shell function or an external file.
- 6:34Speaker 1: There's which which locates the exact folder a command lives in.
- 6:39Speaker 1: And there's appropos, which I think is brilliant if you
- 6:41Speaker 1: can't remember a command's name, you just give it a
- 6:43Speaker 1: keyword and it searches the system's manual pages using regular expressions.
- 6:47Speaker 2: Which is incredibly helpful.
- 6:48Speaker 1: Yeah, and for anyone unfamiliar, a regular expression is essentially
- 6:52Speaker 1: a sequence of characters that acts like a highly advanced,
- 6:55Speaker 1: super powered search pattern. But this brings up a mechanical
- 7:00Speaker 1: question for me. Okay, shoot, when I type something simple
- 7:03Speaker 1: like l's to list my files, how does the shell
- 7:06Speaker 1: actually know where that specific program is located on my
- 7:09Speaker 1: hard drive?
- 7:10Speaker 2: Oh? Well, it relies on an environment variable called the path.
- 7:14Speaker 2: The path Yeah, the path through is simply a text
- 7:17Speaker 2: string containing a list of direct relocations, and they're separated
- 7:21Speaker 2: by colon's. Think of it as the shell's authorized search map.
- 7:25Speaker 1: Okay, a search map.
- 7:26Speaker 2: Right. When you type l's and a hit ender, Bash
- 7:30Speaker 2: doesn't just scan your entire hard drive. That would take
- 7:33Speaker 2: entirely too long.
- 7:34Speaker 1: Oh yeah, that would be agonizing.
- 7:36Speaker 2: Instead, it reads the path variable and checks the first
- 7:39Speaker 2: folder on that list. If it finds an executable file
- 7:43Speaker 2: named l's in that folder, it runs it immediately and
- 7:45Speaker 2: stops searching. If not, it moves to the second folder,
- 7:49Speaker 2: then the third, and so on.
- 7:50Speaker 1: Wait, wait, I have to stop you there, because wouldn't
- 7:52Speaker 1: it be infinitely easier to have the computer check the
- 7:55Speaker 1: folder I'm currently standing in first?
- 7:57Speaker 2: You would think, so, right?
- 7:58Speaker 1: I mean, if I'm working inside a specific project folder
- 8:00Speaker 1: and I type a command, logic dictates the shell should
- 8:03Speaker 1: look right in front of my face before it goes
- 8:05Speaker 1: trekking across the entire system map. Why on earth is
- 8:08Speaker 1: it designed this way?
- 8:08Speaker 2: Well, this raises an important question about the tension between
- 8:12Speaker 2: convenience and security. Let's walk through what happens if we
- 8:15Speaker 2: can figure your path the exact way you just described.
- 8:19Speaker 2: We put your current directory, which Unix represents with a
- 8:22Speaker 2: single dot. By the way, at the very front of
- 8:24Speaker 2: your path makes sense to me. So the shell will
- 8:26Speaker 2: now check whatever fold do you happen to be in
- 8:28Speaker 2: before it checks the official system.
- 8:30Speaker 1: Folders, which sounds incredibly efficient.
- 8:33Speaker 2: Until you wander into a directory created by someone with
- 8:35Speaker 2: malicious intent. Oh no, yeah, a bad actor knows that
- 8:40Speaker 2: systems administrators frequently take LS to see what is inside
- 8:44Speaker 2: a newly downloaded folder.
- 8:46Speaker 1: It's muscle memory, right, you enter a folder, you type LS.
- 8:50Speaker 2: Exactly, So this bad actor writes a highly destructive script,
- 8:55Speaker 2: Maybe something that silently deletes databases or steals passwords. And
- 8:59Speaker 2: they say that script in their folder and they name
- 9:02Speaker 2: it els.
- 9:02Speaker 1: Oh, I see where this is going.
- 9:04Speaker 2: You navigate into their trap folder. You type ls fully
- 9:07Speaker 2: expecting to see a list of files, But because your
- 9:09Speaker 2: path is configured to check your current directory first, the
- 9:12Speaker 2: shell finds their malicious script named l's and executes it
- 9:16Speaker 2: using your account privileges. That is terrifying, and the shell
- 9:20Speaker 2: stops searching instantly. It never even reaches the real secure
- 9:25Speaker 2: Indel's program that's located deep in the system's binary folders.
- 9:29Speaker 1: Wow.
- 9:30Speaker 2: Yeah. By forcing the path to search the official lockdown
- 9:34Speaker 2: system directories first, the architecture guarantees you are running the
- 9:38Speaker 2: genuine tools, regardless of what traps someone left in your
- 9:41Speaker 2: current working folder.
- 9:42Speaker 1: That is beautifully devious and a phenomenal reason to never
- 9:46Speaker 1: ever put the dot directory at the front of your path.
- 9:49Speaker 2: Exactly never do it.
- 9:50Speaker 1: So Okay, we know how the shell safely locates and
- 9:53Speaker 1: triggers commands, but triggering a command is only half the battle.
- 9:57Speaker 1: We need to deal with the data those commands.
- 9:59Speaker 2: Produce, right, the actual output.
- 10:01Speaker 1: Yeah, and this introduces us to what is arguably the
- 10:03Speaker 1: most famous core philosophy of Unix.
- 10:06Speaker 2: Everything is a file, yes, the Golden rule.
- 10:09Speaker 1: The cookbook emphasizes that. To the operating system, a hard
- 10:12Speaker 1: drive is a file, a terminal window is a file,
- 10:15Speaker 1: and your keyboard is a file. The system treats all
- 10:18Speaker 1: of them as just an ordered sequence of bytes. But
- 10:20Speaker 1: I have to push back on that though. Calling everything
- 10:22Speaker 1: a file sounds incredibly reductive. Like a keyboard has physical
- 10:27Speaker 1: mechanical switches, a hard drive has spinning magnetic platters. Treating
- 10:32Speaker 1: them is the exact same thing. Seems like you're stripping
- 10:34Speaker 1: away all the functionality that makes them useful.
- 10:37Speaker 2: Well, what's fascinating here is that the stripping away is
- 10:41Speaker 2: the entire point. Really, Yeah, that radical simplification is exactly
- 10:45Speaker 2: what gives the command line its power. Yes, the physical
- 10:48Speaker 2: hardware is vastly different, but remember are split between the
- 10:52Speaker 2: kernel and the shell earlier, right.
- 10:54Speaker 1: The engine and the steering wheel.
- 10:55Speaker 2: Exactly. Yeah, the kernel handles the magnetic platters and the
- 10:59Speaker 2: mechanical switches. It translates those physical actions into a simple
- 11:03Speaker 2: stream of digital bytes. So by the time that data
- 11:06Speaker 2: reaches the shell, all that hardware complexity is just gone.
- 11:09Speaker 2: Oh I get it, because the shell treats everything as
- 11:12Speaker 2: a simple file stream. A software developer doesn't need to
- 11:15Speaker 2: know how to program a driver for a specific brand
- 11:18Speaker 2: of keyboard or a specific model of monitor.
- 11:21Speaker 1: They just write a program that reads bytes from an
- 11:23Speaker 1: input file and writes bites to an output file.
- 11:26Speaker 2: Precisely, the shell seamlessly connects those files to the physical hardware.
- 11:31Speaker 1: Which means before we start passing these bytes streams around,
- 11:34Speaker 1: we really have to talk about quoting.
- 11:35Speaker 2: Oh.
- 11:36Speaker 1: Quoting is crucial because if you just type raw text
- 11:39Speaker 1: into the shell, it will try to interpret it. The
- 11:41Speaker 1: cookbook uses this fantastic example trying to get the shell
- 11:45Speaker 1: to print the sentence a coffee is five dollars.
- 11:49Speaker 2: Right with the dollar sign and the exclamation point.
- 11:51Speaker 1: Yeah, if you wrap that sentence in double quotes, the
- 11:55Speaker 1: shell tries to be entirely too helpful. It sees the
- 11:58Speaker 1: dollar sign and thinks, aha, that is a variable. I
- 12:01Speaker 1: need to swap five dollars out for whatever value is
- 12:04Speaker 1: stored in memory.
- 12:05Speaker 2: But there is no variable named five.
- 12:06Speaker 1: Exactly, so it replaces five dollars with absolutely nothing. Then
- 12:10Speaker 1: it scans further sees the exclamation point. Assumes you want
- 12:13Speaker 1: to run a history substitution to pull up a previous command,
- 12:16Speaker 1: and the whole thing just throws an error.
- 12:18Speaker 2: It's chaos. The shell basically acts like an overeager assistant
- 12:22Speaker 2: trying to interpret your words before passing them to the
- 12:25Speaker 2: actual program.
- 12:26Speaker 1: An over eager assistant.
- 12:27Speaker 2: That's perfect, yees. So if you want the assistant to
- 12:29Speaker 2: back off and take you literally, you use single quotes.
- 12:32Speaker 1: Single quotes.
- 12:33Speaker 2: Single quotes form an impenetrable barrier. They completely disable the
- 12:37Speaker 2: shell's ability to interpret special characters, so the dollar sign
- 12:41Speaker 2: and the exclamation point are treated merely as shapes on
- 12:44Speaker 2: the screen. What you type inside single quotes is exactly
- 12:48Speaker 2: what gets passed along.
- 12:49Speaker 1: Okay, so here's where it gets really interesting. Once your
- 12:52Speaker 1: text is protected and your commands are running, you can
- 12:55Speaker 1: start building the plumbing.
- 12:56Speaker 2: The plumbing.
- 12:57Speaker 1: Yes, we use the greater than sign to read diirect
- 13:00Speaker 1: output into a text file, a double grater than to
- 13:03Speaker 1: append to the bottom of an existing file, and the
- 13:06Speaker 1: less than sign to pull input into a program from a.
- 13:09Speaker 2: File standard redirection.
- 13:10Speaker 1: Right, But the show is actually performing some incredibly clever
- 13:14Speaker 1: deception behind the scenes.
- 13:15Speaker 2: Here.
- 13:16Speaker 1: The cookbook highlights this bizarre anomaly with the l's command
- 13:19Speaker 1: that completely caught me off guard.
- 13:21Speaker 2: Oh, the column formatting.
- 13:22Speaker 1: Yes, if you type l's rows directly into your terminal,
- 13:25Speaker 1: it formats your files into neat wide columns so they
- 13:28Speaker 1: fit perfectly on your monitor. But if you type l's
- 13:31Speaker 1: and use the greater then sign to redirect that exact
- 13:34Speaker 1: same output into a text file, the formatting changes.
- 13:37Speaker 2: It does.
- 13:38Speaker 1: You open the file and the columns are just gone.
- 13:41Speaker 1: It's a single, massive, vertical list, one file per line.
- 13:45Speaker 1: How does the program know we redirected it? I mean,
- 13:48Speaker 1: how does it No, it's not looking at a screen anymore.
- 13:51Speaker 2: So to understand the mechanism behind that, we have to
- 13:53Speaker 2: talk about file descriptors.
- 13:54Speaker 1: Okay, file descriptor.
- 13:55Speaker 2: A file descriptor is basically just an ID tag, a
- 13:58Speaker 2: simple number the operating system assigns to an open data stream,
- 14:03Speaker 2: and by default, every single program gets three of these
- 14:06Speaker 2: ID tags the moment it starts. Just three, just three.
- 14:10Speaker 2: File descriptor zero is standard input, which is usually your keyboard,
- 14:13Speaker 2: file scripture one is standard output, usually your screen, and
- 14:16Speaker 2: file descriptor two is standard air, also usually your screen.
- 14:19Speaker 1: Okay, so zero, one and two, right, So.
- 14:21Speaker 2: When you use the greater than sign, you are telling
- 14:24Speaker 2: the shell take file descriptor one, disconnect it from the
- 14:27Speaker 2: monitor and plug it into this text file instead.
- 14:30Speaker 1: Wait, the redirection is supposed to be invisible to the program,
- 14:33Speaker 1: right the program just keeps pushing bytes to descriptor one.
- 14:36Speaker 2: Generally, yes, that's the beauty of it. But programs are
- 14:39Speaker 2: actually allowed to ask the operating system, hey, what exactly
- 14:42Speaker 2: is my file descriptor one plugged into? Right now? O?
- 14:45Speaker 1: Way?
- 14:46Speaker 2: Yeah, The developers of L's added a tiny check. The
- 14:50Speaker 2: program queries the OS. If the OS replies, you are
- 14:53Speaker 2: plugged into a terminal display, then LS calculates the width
- 14:57Speaker 2: of the screen and neatly arranges the columns for human eyes.
- 15:00Speaker 1: But if it's plugged into a file exactly.
- 15:03Speaker 2: If the ALWAYS replies you are plugged into a flat
- 15:05Speaker 2: text file, L's knows the human isn't reading it right now.
- 15:08Speaker 2: It knows another program is likely going to parse that
- 15:11Speaker 2: file later, and parsing a single predictable vertical list is
- 15:15Speaker 2: drastically easier for a computer than trying to untangle columns.
- 15:19Speaker 1: Is that underlying logic is brilliant. It literally adapts to
- 15:22Speaker 1: the medium it's being sent to.
- 15:23Speaker 2: It's very smart.
- 15:24Speaker 1: And speaking of plumbing, that brings us back to those
- 15:27Speaker 1: file descriptors you mentioned specifically standard output which is one,
- 15:30Speaker 1: and standard error which is two. This perfectly explains a headache.
- 15:34Speaker 1: I think literally everyone.
- 15:35Speaker 2: Encounters all the error bleed.
- 15:37Speaker 1: Yes, you run a command that generates a massive wall
- 15:40Speaker 1: of text, including a bunch of error messages. You decide
- 15:44Speaker 1: to save it, so you use the greater than sign
- 15:46Speaker 1: to redirect the output to a file. Right, you hit enter,
- 15:50Speaker 1: and all the normal text goes quietly into the file,
- 15:52Speaker 1: but the error message is still bleed all over your screen.
- 15:55Speaker 1: It's so frustrating, it.
- 15:57Speaker 2: Is, But that happens because the sta entered greater than
- 16:00Speaker 2: sign only redirects file descriptor one. Oh yes, Stream one
- 16:05Speaker 2: goes to the file, but stream two, the errors remains
- 16:08Speaker 2: plugged directly into your monitor. To catch the errors, you
- 16:12Speaker 2: have to explicitly redirect descriptor two by typing too.
- 16:15Speaker 1: Ah okay.
- 16:16Speaker 2: But the cookbook also highlights a vital, highly technical nuance
- 16:20Speaker 2: regarding how these two streams deliver their payloads. And that's buffering, right, buffering.
- 16:24Speaker 1: So standard output is buffered, meaning it acts like a bucket.
- 16:28Speaker 1: The program doesn't send data to the screen one letter
- 16:30Speaker 1: at a time. It fills up a memory bucket, and
- 16:32Speaker 1: when the bucket is full, it dumps the whole chunk
- 16:35Speaker 1: to the scream at once.
- 16:36Speaker 2: Which is highly computationally efficient.
- 16:37Speaker 1: Yeah, exactly, But Standard error Descriptor two is unbuffered. Every
- 16:43Speaker 1: single character is transmitted the exact millisecond it is generated.
- 16:47Speaker 1: Why treat the errors differently?
- 16:48Speaker 2: It comes down to crash survival.
- 16:50Speaker 1: Crash survival.
- 16:51Speaker 2: Yeah, imagine a program encounters a catastrophic fatal error. It
- 16:56Speaker 2: generates an error message detailing exactly what went wrong. But
- 17:00Speaker 2: if that message goes into a buffer bucket and the
- 17:03Speaker 2: program crashes before the bucket gets dumped, the bucket just
- 17:06Speaker 2: vanishes exactly that error message is destroyed in memory. You're
- 17:10Speaker 2: left staring at a dead program with zero clues as
- 17:13Speaker 2: to why it died.
- 17:14Speaker 1: That makes total sense.
- 17:15Speaker 2: By making standard error entirely unbeffored, the system guarantees that
- 17:19Speaker 2: the diagnostic text prints to your screen instantly. Even if
- 17:22Speaker 2: the program violently terminates a fraction of a millisecond later,
- 17:26Speaker 2: the error message has already made it out safely.
- 17:28Speaker 1: It prioritizes survival of the forensic data over efficiency. I
- 17:32Speaker 1: love that. And you know, if a program is just
- 17:34Speaker 1: obnoxiously chatty and you don't want to see its output
- 17:36Speaker 1: or save it. You can just redirect the stream to devnel.
- 17:39Speaker 2: Ah. Yes, the bitbucket.
- 17:41Speaker 1: It's a literal black hole where data goes to be
- 17:44Speaker 1: permanently deleted. So okay, we've mastered basic plumbing. We can
- 17:49Speaker 1: route output and errors to files or black holes. But
- 17:52Speaker 1: a shell's real power isn't just shuffling data into files, right,
- 17:56Speaker 1: it's chaining tools together.
- 17:58Speaker 2: Yes, this is where we introduce pipes, represented by the vertical.
- 18:02Speaker 1: Bar character the pipe.
- 18:04Speaker 2: A pipe takes the standard output of one command and
- 18:06Speaker 2: plugs it directly into the standard input of the next command.
- 18:09Speaker 2: But the mechanism of how they execute is what makes
- 18:12Speaker 2: them so powerful. A pipe does not act like a
- 18:15Speaker 2: relay race. What do you mean by that, Well, it
- 18:17Speaker 2: does not wait for the first program to finish its job,
- 18:20Speaker 2: save the data, and hand a completed file over to
- 18:22Speaker 2: the second program. A pipe is an assembly line.
- 18:25Speaker 1: Oh, they run at the same time.
- 18:26Speaker 2: Both programs launch and run simultaneously. The first program writes
- 18:30Speaker 2: data into a shared memory buffer, and the second program
- 18:33Speaker 2: continuously reads from that exact same buffer in real time.
- 18:37Speaker 1: That parallel processing is why the command line can handle
- 18:40Speaker 1: massive data sets. So quickly. You aren't constantly reading and
- 18:43Speaker 1: writing temporary files.
- 18:45Speaker 2: To the hard drive exactly. It's all in memory.
- 18:47Speaker 1: But because data flows too easily, the cookbook mentions, users
- 18:51Speaker 1: often fall into a trap called the useless use of
- 18:53Speaker 1: cat award.
- 18:54Speaker 2: Oh this is a classic.
- 18:55Speaker 1: Yeah, so, pat is a simple command designed to concatenate
- 18:58Speaker 1: files and dump their content to standard output. Beginners constantly
- 19:03Speaker 1: use cat to open a file and then pipe that
- 19:05Speaker 1: text into a search tool like grep, but GP is
- 19:08Speaker 1: fully capable of reading the file directly. The pipe is
- 19:11Speaker 1: just burning CPU cycles for absolutely no reason.
- 19:14Speaker 2: It is although from a purely psychological standpoint, constructing commands
- 19:19Speaker 2: from left to right, starting with the file and then
- 19:21Speaker 2: piping it through various filters, it often aligns better with
- 19:24Speaker 2: how the human brain visualizes a sequence of events.
- 19:28Speaker 1: That's a fair point. It feels more intuitive, but.
- 19:30Speaker 2: Yes, technically it is unnecessary overhead.
- 19:33Speaker 1: Okay, so if we have this beautiful high speed assembly
- 19:36Speaker 1: line of data flowing through pipes, what if we want
- 19:39Speaker 1: to intercept that data without stopping the flow, like tapping
- 19:42Speaker 1: the pipe to check the water quality halfway down the line,
- 19:45Speaker 1: to use.
- 19:45Speaker 2: A t joint a t jup. The command is literally
- 19:48Speaker 2: named t You insert TEA into the middle of your
- 19:50Speaker 2: pipe chain. It reads the data flowing through, saves an
- 19:54Speaker 2: exact copy of it to a text file you specify,
- 19:57Speaker 2: and simultaneously passes the original data completely untouched, down the
- 20:01Speaker 2: pipe to the next program.
- 20:03Speaker 1: Wow.
- 20:03Speaker 2: It's an invaluable tool for debugging a complex multi step
- 20:07Speaker 2: workflow because you can inspect exactly what the data look
- 20:11Speaker 2: like at step three before winn into step four.
- 20:14Speaker 1: That is so useful. Another powerful way to chain things
- 20:17Speaker 1: is command substitution. You wrap a command and a dollar
- 20:20Speaker 1: sign in parentheses. The shell runs the command inside the parentheses,
- 20:24Speaker 1: first captures the output, and pastes that output directly into
- 20:27Speaker 1: your current command line as arguments. For example, you could
- 20:31Speaker 1: use the fine command to locate all temporary files and
- 20:34Speaker 1: use command substitution to instantly feed that list of files
- 20:37Speaker 1: to the ARM command to delete them.
- 20:38Speaker 2: Though you must exercise extreme caution with that. Why because
- 20:42Speaker 2: if your fine command is formulated incorrectly and it returns
- 20:45Speaker 2: a list of critical system files instead of temporary files,
- 20:48Speaker 2: the ARM command will unquestioningly delete.
- 20:50Speaker 1: All of them, oh right, which perfectly transitions us to
- 20:54Speaker 1: conditional execution. We need safety valves.
- 20:57Speaker 2: We definitely do Instead of.
- 20:58Speaker 1: Just running sequences blindly, we can use the double Ampersand
- 21:02Speaker 1: if you put two ampersands between commands, you are telling
- 21:05Speaker 1: the shell run the second command only if the first
- 21:08Speaker 1: command succeeds.
- 21:10Speaker 2: The quintessential example of this is changing directories before initiating
- 21:13Speaker 2: a deletion. Okay, consider the command CD nir roma. The
- 21:20Speaker 2: shell attempts the CD command first. Let's say the nie
- 21:23Speaker 2: kaik befolder was accidentally deleted yesterday, or you simply misscalded.
- 21:27Speaker 2: The CD command fails and throws an error code, and
- 21:30Speaker 2: then what the double ampersand sees that failure and instantly
- 21:33Speaker 2: aborts the entire chain. If you had just separated those
- 21:35Speaker 2: commands with a basic semicolon, the shell would shrug at
- 21:38Speaker 2: the failed CD command blindly, move on to ourre and
- 21:41Speaker 2: proceed to delete every single file in whatever directory you
- 21:45Speaker 2: currently happen to be.
- 21:45Speaker 1: Standing in, which could be catastrophian exactly.
- 21:48Speaker 2: The amper sans check the exit status, and if it
- 21:50Speaker 2: isn't a zero, which mathematically indicates success in Unix, it
- 21:54Speaker 2: halts the execution.
- 21:55Speaker 1: And speaking of preventing disasters, what about the most common
- 21:58Speaker 1: mistake of all you tie but greater than sign to
- 22:01Speaker 1: redirect some output, but you accidentally type the name of
- 22:04Speaker 1: an incredibly important file that already exists. Yeah, the shell
- 22:09Speaker 1: will instantly overwrite your important file with the new data,
- 22:12Speaker 1: permanently destroying the old contents.
- 22:15Speaker 2: Well, you can configure a safety net for that by
- 22:17Speaker 2: typing set toch no clauber into your terminal no clawber. Yeah.
- 22:21Speaker 2: This instructs Bash to strictly refuse to overwrite any existing
- 22:25Speaker 2: file if you use a standard redirection symbol.
- 22:28Speaker 1: But what if I'm completely sure I want to overwrite it?
- 22:30Speaker 1: Like what if I am updating a log file and
- 22:32Speaker 1: I know exactly what I'm doing.
- 22:33Speaker 2: You don't have to turn the safety net off. You
- 22:36Speaker 2: just use the override syntax a greater than sign followed
- 22:39Speaker 2: immediately by a vertical bar. Oh.
- 22:41Speaker 1: Okay.
- 22:42Speaker 2: That vertical bar is a direct command to the shell
- 22:45Speaker 2: to ignore the no clabber rule for this specific execution.
- 22:48Speaker 2: It shares the same imperative do it anyway, regardless of
- 22:51Speaker 2: the consequences, spirited the exclamation point that you'd use to
- 22:55Speaker 2: force actions in the vitext editor.
- 22:57Speaker 1: Okay. I have to push back on one final concept
- 23:00Speaker 1: from the cookbook, though, because it just seems unnecessarily complicated,
- 23:03Speaker 1: All right, what is it? They dedicate a whole section
- 23:05Speaker 1: to here documents. This involves using two less than signs
- 23:10Speaker 1: and a marker word like eof to embed large blocks
- 23:13Speaker 1: of raw texts directly inside the code of a shell script.
- 23:18Speaker 2: Hu docs.
- 23:18Speaker 1: Why go through the trouble of embedding data directly into
- 23:21Speaker 1: the script with weird boundary tags? Why not just keep
- 23:24Speaker 1: your data in a clean, separate text file and have
- 23:27Speaker 1: the script read from.
- 23:28Speaker 2: That file because of the elegance of portability portability. Imagine
- 23:31Speaker 2: you write a script that requires a small static lookup table,
- 23:35Speaker 2: perhaps a list of server IP addresses or basic configuration values.
- 23:39Speaker 2: If you keep that data in a separate file, you
- 23:42Speaker 2: now have two files to manage.
- 23:43Speaker 1: True.
- 23:44Speaker 2: If you email the script to a colleague, you have
- 23:46Speaker 2: to remember to attach the data file. If you move
- 23:49Speaker 2: the script to a different folder, the hard coded filepath breaks.
- 23:52Speaker 2: A here document allows you to package the executable code
- 23:55Speaker 2: and the necessary static data into one single, self contained unit.
- 24:00Speaker 2: It guarantees the script always has the data it needs
- 24:02Speaker 2: to run.
- 24:03Speaker 1: Okay, that does make sense from a distribution standpoint, but
- 24:05Speaker 1: the cookbook highlights a really bizarre Parson quirk with these
- 24:09Speaker 1: here documents that can just ruin your data.
- 24:11Speaker 2: Oh, the variable expansion issue.
- 24:13Speaker 1: Yes, Let's say I'm writing a script that generates a report,
- 24:17Speaker 1: and inside my embedded text block, I write a line
- 24:19Speaker 1: that says pete one hundred dollars to represent a donation.
- 24:22Speaker 1: Right when the script actually runs and prints the text,
- 24:25Speaker 1: it spits out pete zero zero, the one completely vanished.
- 24:30Speaker 1: Where did it go?
- 24:31Speaker 2: Well, this is where understanding the shell's order of operations
- 24:33Speaker 2: is vital. By default, the shell looks inside the here
- 24:37Speaker 2: document and expands any variables it recognizes before it does
- 24:41Speaker 2: anything else.
- 24:42Speaker 1: Oh, so it sees the dollar sign exactly.
- 24:44Speaker 2: It scans your text and seas one hundred dollars. It
- 24:46Speaker 2: interprets the one dollar as a variable representing the first
- 24:49Speaker 2: argument pass to the script. And if you just ran
- 24:52Speaker 2: the script normally without passing any arguments, one dollar is
- 24:55Speaker 2: completely empty. So the shell dynamically replaces the one dollar
- 24:59Speaker 2: with nothing, leaving only the two trailing zeros.
- 25:02Speaker 1: So my one hundred dollars string is silently mutated into
- 25:05Speaker 1: zero dollars. How do you stop the shell from doing that?
- 25:08Speaker 2: With one single backslash?
- 25:09Speaker 1: Just a backslash?
- 25:11Speaker 2: Yep, When you declare the start of your hearge document.
- 25:13Speaker 2: Instead of typing eof you type eof. That single backslash
- 25:18Speaker 2: acts as a master switch. It signals the shell to
- 25:21Speaker 2: completely disable variable expansion inside the document block. Wow, the
- 25:25Speaker 2: shell is forced to treat everything inside as raw literal text.
- 25:29Speaker 2: One keystroke completely changes how the entire script parses the data.
- 25:33Speaker 1: So what does this all mean? We started today by
- 25:36Speaker 1: stepping through that hidden door, pulling back the curtain on
- 25:39Speaker 1: what a shell actually is, a swappable interface entirely separated
- 25:43Speaker 1: from the heavy lifting kernel. We learned how to map
- 25:46Speaker 1: this new environment using the path variable. Discovering the very
- 25:49Speaker 1: real security threats that dictate how folders are ordered. We
- 25:53Speaker 1: challenge the everything is a file mantra, only to realize
- 25:56Speaker 1: that treating hardware as simple byte streams is what enables
- 25:59Speaker 1: us to build massive plumbing networks. With file descriptors, we
- 26:02Speaker 1: unpacked the survival mechanics of unbuffered error streams, and we
- 26:06Speaker 1: connect it all with parallel processing pipes, t joints, and
- 26:09Speaker 1: strict conditional logic. It means that by understanding a few
- 26:13Speaker 1: simple underlying mechanisms, you can combine tiny specialized commands into
- 26:19Speaker 1: incredibly powerful automated workflows. You are no longer just a
- 26:24Speaker 1: passenger clicking through menus on your computer. You are the
- 26:27Speaker 1: mechanic who understands the wiring, which.
- 26:29Speaker 2: Leads to a rather profound realization about the current trajectory
- 26:33Speaker 2: of modern computing. Oh, we are living in an era
- 26:35Speaker 2: where tech companies are racing to build AI driven interfaces
- 26:39Speaker 2: that attempt to guess what you want. They're designed to
- 26:42Speaker 2: interpret your vague intentions, smooth over your mistakes, and execute
- 26:46Speaker 2: tasks on your behalf.
- 26:47Speaker 1: That's definitely the trend right now.
- 26:49Speaker 2: Right, But the command line remains the exact polar opposite
- 26:52Speaker 2: of that trend. It is brutally literal. It does exactly
- 26:55Speaker 2: what you tell it to do, no more, no less,
- 26:57Speaker 2: even if what you tell it to do is catastrophic.
- 26:59Speaker 1: Yeah, we definitely saw that with the root access and
- 27:01Speaker 1: the overriting exactly.
- 27:03Speaker 2: So it forces us to ask a difficult question. As
- 27:06Speaker 2: our daily interfaces become smarter, more predictive, and vastly more abstract,
- 27:11Speaker 2: what fundamental computing powers and deep understandings of our own
- 27:15Speaker 2: machines are we voluntarily giving up?
- 27:17Speaker 1: Wow? If we rely entirely on the graphical illusions and
- 27:20Speaker 1: the predictive guessing games, we inevitably forget how the underlying
- 27:24Speaker 1: engine actually works. We hand over the master keys. You do,
- 27:27Speaker 1: but for today, at least you have a few more
- 27:29Speaker 1: raw commands in your back pocket to ensure that doesn't happen.
- 27:32Speaker 1: Stay curious, keep exploring that blank screen, and thanks for
- 27:36Speaker 1: taking the deep dive with us today