Hello Linux (and Bash) My Old Friends (to the tune of The Sound of Silence)
Hello Linux (and Bash) My Old Friends (to the tune of The Sound of Silence)
This week kicked off a journey to finally conquer a beast that has been nagging at my ear for the past four years or so. For the past four years, I’ve been incredibly fascinated by the cloud. Its robust capabilities and modernization of old school tech has always made it the shiny new toy that I’ve always wanted to look at. But in the past the journey usually went “I’m gonna do it” to overwhelmed with acronym soup flooding my brain in a matter of a week. As I’ve been on this learning journey, I’m always brought back to one thing that I stubbornly forget every time I choose something new to explore: dumb dumb, don’t overlook the fundamentals which is way easier said than done in an age of AI. So today, I spent my day off working on the one thing I’ve worked on before but never looked at deeply: Linux and bash scripting.
My learning session today started like every other. I went through my starting routine of pulling up content (shout out to learntocloud.guide and Claude) and getting all my digital ducks in a row. First up was learning the history of linux - which is insane if you haven’t had the chance to read about it - so I know what the heck I’m even working on. Next was the hands on part: working through the bash tutorial on freecodecamp.org. Since the whole point of this is really learning cloud, I made the strategic decision to use a GCP VM instance as my playground for practicing bash scripting instead of using a docker container or something like VMFusion. It was going perfect and setting up that VM instance and clicking “SSH” quickly became the easiest part of this whole thing.
Once I was in my VM, it was time to actually get to work writing my first script. I did everything by the book (or in this case tutorial). Hit that vi run_all.sh command to create a file in the Vim editor and tried to add in my shebang to the top of the script and boom. My first obstacle has come: why was I getting an error when trying to add in a simple ‘#” character? Was I doing something wrong?
Spoiler alert. Yes. Yes I was. As a personal believer that AI can be an incredibly powerful tutor if used correctly, I went to Claude to see what I was missing. Through the back and forth, something about Vim finally clicked. It’s powerful because of the different modes it has. I wasn’t in the mode that I needed. When you open a document in Vim, it’s automatically opened in the command mode. This mode’s super power isn’t just navigating the document with ease, it’s actually the mode’s ability to perform mass operations like deleting whole lines. And thank goodness for that because the mode I needed, insert mode, is AWFUL at most things that aren’t just typing text.
Once I switched to the right mode I needed (shout out i ), it was smooth like butter. I was able to see the parallels with Python and just hit the ground running with the script that looks at a path that the user inputted and gives you the files and folders in that path. Now came the moment of truth. Will my script work? I enter sh run_all.sh, cross my fingers and watch as the output is shown on my screen, and below for you.
I mean it was technically right but why was the -e flag showing up? THAT’S NOT WHAT THE TUTORIAL SAID WOULD HAPPEN (famous last words)! So I go on to investigate what the heck was going on. Turns out, the execution command does a LOT in deciding your output. More than I ever really thought about. Because I was using the sh command, my computer was interpreting this as “I have to run this, doesn’t matter on what”. The -e flag tells echo to interpret escape sequences like \n for new lines. But when I used sh instead of bash, it didn't handle that flag the same way - it just printed it as literal text. I tried ./run_all.sh and magic. NO random -e flags! I had to tell my computer to actually look at the shebang and run it off of what I said.
Now that I knew how basic processes worked, I wanted to dig deeper and work on the things that always give me trouble no matter what language I pick up: good ole loops and cases. In theory, I get what each thing is trying to do but writing them out isn’t always the most fun or easiest for me. So I put on my big girl pants and asked Claude to give me a challenge that I could do to get a better grasp on these things because they aren’t going away any time soon. The challenge was to create a script that asks the user to see a menu. If they choose yes, show the user a list of three foods and ask the user to pick one. Then use a case statement to tell the user the price of that food and ask if they want to see the menu again. This loop continues on until they say no.
Once I saw the similarities between how bash scripts and python scripts are written, it was night and day how much easier bash scripting got. But it also taught me a few quirks that I have to remember when writing bash scripts.
- Like in everything, syntax matters: When I was writing my while loop, I overlooked the comparison condition and was getting an error that was preventing my script from actually working like a loop. The different operators and ways you use them, tells the computer how to treat the comparison. Despite what my intuition says, flags tell the computer “treat these as numbers”, and == or > tell the computer to treat these as text.
- Test EVERYTHING: I was coming into this learning session assuming that bash and other programming languages act exactly the same. I would know when errors would be thrown and when things would just keep rolling. If I hadn’t gone back and tested each section with edge cases, there’s no way I would have known that if you enter in a menu item that wasn’t an option, my loop would just keep going.
- Coding and scripting is just pattern recognition: Through this exercise, I found myself thinking back on the python fundamentals and how this fits hand in hand with bash scripting. My brain walked in confused but once I saw the similarities and patterns, it felt much more manageable.
TLDR:
Today felt like a true start to conquering the annoying beast that’s been in my ear the past four years. I was able to take what I learned from python and translate that into a working script. Vim finally finally makes sense. And I think I found a way to learn that actually satiates my neurospicy brain: don't stifle the natural rabbit holes, just shift them. Curious about something tangential? Bookmark it. Come back during free exploration time. That way the dopamine hits keep coming without derailing the main quest. Next up: back to basics of my biggest mountain in cloud learning so far. Time to conquer networking.