Coding Education Events and Life

Parenting moments with shell scripting

I’m both fascinated by and borderline besotted with shell scripting (you know, all that command line stuff). It turns out my son is intrigued by what you can do from the command line, and it opens the door to some pretty cool parenting moments.

This afternoon I showed my son a little script I wrote that opens Firefox Developer Edition, VS Code, Remember the Milk, and displays the weather forecast for my city just by running an aliased script from my command line. The weather service is a very cool command line script that Jessie Frazelle wrote.

My son was astounded when he saw all these apps open after typing a single command in iTerm2. He immediately asked me to create something for him.

So I wrote a similar script for him for his Ubuntu PC. It doesn’t work quite the same as mine does on my Mac (Linux has an interesting way of opening some apps that tethers them to the terminal session) but he now has a little command he can use to open VS Code and give him a weather update from his terminal.

I love that he’s also into these things. He asked me to introduce him to something he can use to learn all these shell commands. He wants to write them down in a sort of cheatsheet. I have a couple of resources (the Linux Documentation Project is an awesome one I only recently came across) I’ll share with him, I think he’ll get a kick out of learning this stuff.

Photo by Lewis Ngugi on Unsplash
Coding Useful stuff

My favourite shell command for today – prepend lines of text in seconds

I’m working on a lead generation project for a client this week that, interestingly, led me to a simple, time-saving shell command that enables me to prepend lines of text with a simple quotation mark. It’s more impressive than it sounds because it saved me considerably more time than it took to write and run it.

One of the methods I’m using to find leads is by running Boolean searches that incorporate multiple search parameters. The search terms are pretty long and look a little like this: "Managing director" OR "sales director" OR "sales manager" OR "procurement manager" OR ...

My starting point is generally a list of search parameters that I need to convert into a Boolean search query. For example:

Managing director

sales director

sales manager

procurement manager

Except the lists are a lot longer. I started off by adding my lists to VS Code because working with this sort of thing in plain text greatly simplifies the process. One of the things I love about VS Code is that you can add a cursor to the end of each line in the text you select. This probably comes from Sublime Text and it’s an awesome feature.

The reason why this comes in handy is because I want to place each of those search terms into quotes so I can string them together with Boolean operators like AND, OR, and NOT.

The challenge I found myself facing is how to move the cursor to the beginning of each like to add the opening quotation mark? Because each line is a different length, I couldn’t just move the cursors from the end of each line to the beginning. I also didn’t want to overwrite the feature that lets me add cursors to the end of each line, either.

I thought that there must be some sort of shell command or script I could run that would parse the text file and add an opening quotation mark to the beginning of each line. It turns out there is (of course). Like many shell commands, it is deceptively simple and does exactly what it says on the box (if there was a box).

There are three options (probably more if you want something different) that I looked at:

sed -e 's/^/prefix/' file

# If you want to edit the file in-place
sed -i -e 's/^/prefix/' file

# If you want to create a new file
sed -e 's/^/prefix/' file >

I went with option 3 and simply replaced the word prefix with my opening quotation mark and ran the command. I output the command to a new file because I didn’t want to destroy my original list. It ran instantly and when I opened the new file, I had quotation marks at the beginning of each line.

From there it was a relatively simple matter to add cursors to the end of each line to add my Boolean operator for my searches.

Using shell commands may seem absurdly geeky when we have GUIs for just about everything, but the more I learn about shell scripting, the more it amazes me. This particular line of code seems innocuous enough but it did something for me in seconds that would otherwise have taken a lot longer.