Speaker 1: Okay, let's unpack this. Today, we're diving into a topic that uh might sound a little technical at first glance, maybe, but it's incredibly powerful for anyone who wants to make their computer do more work for them. We're talking shell scripting. Think of it as like leveraging your computer's power to automate routine processes. You solve those complex or repetitive digital puzzles just once, and then your machine handles them reliably whenever you need it. So, our mission today really is to demystify shell scripting a bit, equip you with the basics, you know how it works and maybe even inspire you to write your very first script. We've been looking at some great sources. A YouTube FAQ covering variables, an intro to shell scripting guide, and a quick TLDDR video summary. All good stuff to get you up to speed fast. Speaker 2: Yeah. And what's fascinating here, I think, is that shell scripting isn't just about writing code in the traditional sense. It's really about efficiency, consistency, and um smart problem solving. It's a fundamental skill. Really doesn't matter if you're managing a personal project, streamlining data analysis, or even troubleshooting complex networks. It just lets you build these reliable, repeatable solutions without having to do everything by hand every single time. Speaker 1: Okay, so before we get into the how, let's tackle the what and why. What exactly is a shell script? And maybe more importantly, why should someone listening, you know, care about learning this? Speaker 2: Right? So, at its core, a shell script is really just a simple program. It's basically a text file. You list commands in it, one per line, just like you type them into your terminal. But the yare part, that's where it gets interesting. If we connect this to the bigger picture, uh, scripts are fantastic for automating repetitive stuff. Think beyond just simple backups. Imagine a script that not only copies files, but like intelligently rotates your backups. Maybe it keeps recent versions, some monthly archives, automatically managing storage. That's dozens of manual steps saved, right? Speaker 1: Oh, definitely. Speaker 2: And they're also great for those complicated tasks you only do once in a blue moon. You figure it out once, write the script, and boom, you don't have to relearn it. keeps things consistent. Exactly. No forgotten steps. And beyond that, they're really invaluable for basic troubleshooting, say on servers or networks. You can create scripts to check normal operation, establish a baseline, so when something goes wrong, you've got a quick diagnostic tool ready. Speaker 1: So it's like taking those frustrating recurring tasks, and building yourself a custom easy button for them. I like that. All right, so you're convinced you want to make your computer work smarter. Let's get down to actually crafting one. What's the very first step practically speaking? How do you kick things off? Speaker 2: Okay, first step is just creating a file, simple text file. Now, you can name it anything technically, but it's really good practice to add a .sh extension. Something like mycleanupcript.sh. Linux systems don't strictly need that extension like Windows does, but yeah, it's super helpful for you. And for anyone else looking at your files, right, you see, you know immediately it's a shell script. Speaker 1: Right? Clarity for the human makes sense. Okay, file created. What's the first thing we actually write inside it? I've heard about something called The shebang line. Speaker 2: You got it. That very first line is arguably the most important one, the shebang. Typically, you'll see #!/bin/sh or #!/bin/bash. What that line does is tell your system how to execute the script, which program, which interpreter to use. Now, sh is maybe more broadly compatible across different Unix like systems, older systems, simpler systems, but bash the Born Again Shell that offers some extra features that are pretty common these days for most basic scripts. Honestly, the difference might not matter much functionally, but it's crucial the system knows which shell environment to actually use to run your commands. Speaker 1: Got it. So, it's like a directive right at the top. So, that shebang acts like a header telling the system how to interpret everything else. Is there I mean, could you use something else there, not just sh or bash? Speaker 2: Oh, absolutely. That's the beauty of it. You might use #!/usr/bin/python if you wrote a Python script you want to run from the command line or #!/usr/bin/perl for Perl. The shebang is uh kind of a universal way to say run this file using this program. Makes it super flexible for developers using different languages. Speaker 1: Huh. Interesting. Okay. So, we've told the system which interpreter to use. What's the absolute simplest command we can put in there to make it actually do something we can see? Speaker 2: Yeah. Let's start simple. The echo command. That's your go-to. It's basically the "say something" command for your script. So, if you just wanted your script to announce, I don't know, "hello deep dive listeners," you just write echo hello deep dive listeners on a line. Okay. gives you your first tangible output, something you could see happen when you run it. Speaker 1: So, we've got our script saying hello with Echo. Nice and simple. But these scripts can get bigger, right? And we're not just writing for the computer, then we're writing for well for for our future selves, maybe collaborators. How do we make sure we understand what we were trying to do later on? Speaker 2: Good point. That's where comments come in. Anything starting with the hashtag symbol. Speaker 1: Ah, the hash sign. Speaker 2: Exactly. Using comments is just critical best practice. It's like leaving notes for yourself or others. You should definitely put a comment right at the top of your script. Explain what the script does. Maybe put your name, the date. It makes your code so much more readable, maintainable. Imagine trying to figure out some complex script 6 months later with zero notes. Speaker 1: Yeah, not fun. Been there. Not recommended. Okay, that makes perfect sense. So, our script has its shebang. It says hello. It's even documented. Great. We save it. We try to run it and nothing or maybe permission denied. What's the common hurdle there? that tricks people up before the script even runs? Speaker 2: Uh, yes, this definitely catches people out. It raises an important point about how these systems work. By default, when you create a new file, it's not executable. Your computer won't just run any random file as a program. That's a security feature, right? So, you need to explicitly give it permission to run. You do that with a command like chmod +x yourscriptname.sh. Speaker 1: Yep. That +x means add execute permission. It tells the system, hey, treat this file like a program. It's okay to run. It's fundamental on Linux, Mac OS, Unix like systems. Prevents accidental execution. So, you run chmod +x. Is there any way to know it worked like right away besides just trying to run it again? Speaker 2: Often. Yes. And it's kind of a neat little visual cue depending on your terminal settings. Uh the file's name might change color, often to green. Yeah. It gives you that immediate visual confirmation. Okay, the permissions are set. It's executable now. Speaker 1: It's a small thing, but quite satisfying to see. That's a great tip. Okay, so permissions set. Maybe it turned green. Running it should be straightforward now. Speaker 2: Pretty much after you've done the chmod +x, you run it by specifying its path. If the script is right there in your current directory, you type dot followed by the script name. So like ./mycleanupcript.sh. Speaker 1: The dot slash. Speaker 2: Exactly. That ./ is crucial. It means look in the current directory. A really common mistake is just typing the script name without the ./. If you do that, the system usually looks in standard places like /bin or /usr/bin, not your current folder. So it'll say command not found even though the file is right there. You have to tell it where to look. Speaker 1: Right? The path. Got it. Okay. So we've gone from a plain file to an executable script that says hello progress. But you know a static script just doing one fix thing. That's useful but limited. How do we make scripts more dynamic? Maybe have them ask us something. Get input from the user? Speaker 2: Good question. That's where the read command is your friend. If you put read user_response in your script, for example, the script will pause execution right there. It'll wait for you, the user, to type something in and press enter. Speaker 2: Okay? Whatever you type gets stored in a variable, in this case, one named user_response. And that's how your script starts to have a conversation. You know, it's not just talking at you. Speaker 1: And once you have that input stored in the variable, user_response, you can actually use it, right? How do you refer to that stored value later in the script? Speaker 2: You use a dollar sign in front of the variable name. So, $user_response. Speaker 1: $user_response. Our source material had a kind of funny example of this. A script asked, "Would you like to hear a line from Firefly?" It used read answer to get the input. Then it printed out something like, "I don't care that you said answer. I'll tell you anyway." And then gave the quote. So it perfectly showed capturing the input into answer and then displaying it back. Even though in that specific case, the script's logic didn't actually change based on what you typed. Speaker 2: It's a great illustration of the mechanism. read to capture, $ to use. And something important here uh is that shell scripting generally uses what we call untyped variables. Speaker 1: Untyped. What does that mean? Speaker 2: It means unlike a lot of other programming languages, think Java, C++, you don't have to declare what kind of data a variable will hold beforehand. You don't say this variable user_response must be a string or this variable count must be an integer. You just assign a value like read user_response or count=1. The shell figures it out. Speaker 2: Ah, okay. This untyped nature is actually a big part of why shell scripting is so good at like gluing other command line tools together. You can take text output from one command, put it in a variable, feed that variable to another command that expects maybe a number. And often the shell just handles the conversion for you. It makes complex workflows surprisingly simple sometimes. Speaker 1: That flexibility does sound like a bit of a superpower. Yeah. For stitching things together. Speaking of how the shell interprets things, it definitely has its quirks, doesn't it? Yeah. Speaker 2: Especially around quotes. Single quote, double quotes. I hear that can really trip people up. Oh, absolutely. This is subtle. but really really important gets people all the time. What's fascinating, right, is that the type of quote you use fundamentally changes how the shell sees the text inside. There are basically three main ways to handle text. First, plain text, no quotes at all. You say echo hello world. It treats hello and world as separate things, separate arguments, and prints them with a space. Simple. Okay. Speaker 2: Second, double quotes like echo "hello $name". Double quotes are, let's call them, semi-opaque. They group everything inside to a single string, a single argument, but they still allow certain special things to happen inside. Most notably, variable expansion. So if the variable name holds the value you, that command outputs hello you, the variable gets replaced. Speaker 1: It sees the dollar sign and expands it. Speaker 2: Exactly. Now third type, single quotes. Yeah. Like echo 'hello $name'. Single quotes are totally opaque, literal. What you see is exactly what you get. They tell the shell, do not interpret anything special inside here. Treat every character literally. Speaker 1: Mhm. Speaker 2: So, echo 'hello $name' would print exactly that. hello $name. The variable doesn't get expanded. Ah, it's definitely worth playing with these. Make a tiny script, use echo with no quotes, double quotes, single quotes, and see the difference. It clicks once you see it happen. Speaker 1: Okay, that distinction is crucial. No quotes, double quotes for expansion, single quotes for literal. But just when you think you've got quotes sorted, they're also backticks, right? What are those about? Speaker 2: Excellent question. Backticks, the ` character, or actually, the more modern and generally preferred way is dollar sign with parenthesis, $(command). Okay. Yeah. Both do something called command substitution. This is totally different from single or double quotes. What they let you do is run a command within your script, capture its output, and then use that output right there. Like maybe you want to include the current date in a log file name. You could do something like today's_date=$(date). The shell runs the date command inside those parenthesis and whatever text the date command prints out, that gets put directly into into the today's_date variable. Speaker 1: Ah, okay. So, it runs the command and substitutes its output. Speaker 2: Precisely. Yeah, you can achieve the same with backticks like today's_date=\date``. It's a really powerful way to integrate the output of standard tools directly into your script's logic or output. And just a quick practical tip, the backtick key is usually up by the number one key above tab on US keyboards. The single quote is usually near the enter key. Easy to mix them up if you're not careful. Speaker 1: Good tip. Definitely easy to confuse visually. Speaker 2: Yeah. Wow. Okay, we've covered a lot of ground. Creating the file, the shebang, echo, comments for sanity, chmod for permissions, read for input, variables with $, the whole quote zoo, and now command substitution with $(). That feels like a solid toolkit to start with. From your experience looking back at all this, what's the single biggest mistake, the most maybe damaging thing new scriptors do that just derails everything? Speaker 2: That's a good question. If I had to pick one thing, it it raises the most point of all really for any kind of coding. It's testing. You simply have to test your code rigorously. Speaker 1: Test it. Speaker 2: The whole process for making a script work reliably looks something like this. One, test the individual commands you plan to use right there at the command prompt first. Make sure each bit works standalone. Two, then create and edit your script file. Three, put in that shebang line first thing. Four, add those helpful comments at the top function name, date, good habit. Five, save the file. Six, change its permissions chmod +x. Seven, and then crucially test it. Run it. See what happens. Does it do what you expected? And you don't just test once. You test, you tweak, you test again. It's iterative. Rinse and repeat. Especially test small changes. Don't write a 100 lines and then test. Test after every few lines you add. Speaker 1: So, it's not just about avoiding syntax errors, but constantly verifying the logic, the flow, the actual outcome step by step. I've definitely fallen into the trap of writing a bunch of code and thinking, "Oh, this looks right. It should work." Only to find, nope, cascade of problems. Speaker 2: Exactly that. Never ever assume. Never try to use or say turning code you haven't tested. Phrases like, "Well, I think it works or it should be fine. Why wouldn't it?" Those just don't fly. Not in a professional setting. Not in academics. One of our sources even mentioned sometimes needing to prove you tested like with screenshots or pasting output. The message is just crystal clear. Please, please test your code. It saves so much pain down the line. It ensures things actually work as intended. Speaker 1: A powerful final reminder. Okay. Wow. What a deep dive. We went from the what and why of shell scripting, solving those digital puzzles, all the way through crafting a script, shebangs, echo, comments, permissions, getting interactive with read and variables, navigating quotes, and and ending on that absolutely golden rule. Test, test, test. I feel like anyone listening now has a really solid foundation for understanding this incredibly useful tool. Speaker 2: Yeah. Stepping back for a moment, the real power here is that shell scripting is genuinely a foundational skill. It's not niche. It empowers you to go beyond just using your computer to actually teaching it to work smarter for you. And that knowledge unlocks huge efficiency gains, way more precision in tasks. From the really mundane stuff to the seriously complex, it can really transform how you interact with your digital world, giving you back control over repetitive processes. Speaker 1: Absolutely. So, here's a final thought, a provocative one maybe for you to ponder as we wrap up. Think about your own digital life, your own workflows. What's one task, even a small one, that you do over and over again? Maybe it's organizing downloaded files. Maybe it's running a series of checks on a system. Maybe just launching a specific group of apps every morning. Can you identify that puzzle? And now, armed with what we've discussed today, can you start to imagine what a simple shell script might look like to solve that puzzle for you once and for all? How could you automate that away?