DEV Community

mlevesquedion
mlevesquedion

Posted on

7 1

My most important bash script

If you want to do something, make it easy to do.

Applying this principle has allowed me to increase my productivity over time while protecting me from drudgery. Since I spend a lot of time in terminals, a simple example of this is defining appropriate aliases for commands I use a lot, so I don't need to type so much. Sometimes though, an alias doesn't cut it, and I need to write a small script.

Historically, I would cd into a directory on my path, open up a file, write the shebang line, write the script, save it, make it executable, test it, and go back to whatever I was doing. However, since I was always doing those same steps, it made sense to write a script to help me write scripts faster. Here it is :

BIN_PATH="$HOME/bin/"
SCRIPT_NAME="$1"
SCRIPT_PATH="$BIN_PATH$SCRIPT_NAME"

if [ ! -s "$SCRIPT_PATH" ]; then
    echo "#!/bin/bash
" > "$SCRIPT_PATH"
    chmod u+x "$SCRIPT_PATH"
fi

vim "+${2-2}" "$SCRIPT_PATH"

I call it scr, as in script. Say I want to write a new script called foo. All I have to do is type scr foo in a terminal, write the body of the script, save, and I'm done. The next time I want to edit the script, I once again type scr foo, do whatever changes I need to do, save, and I'm done. Since this significantly decreases the effort needed to write scripts, it has allowed me to write more scripts to take care of a variety of tasks I perform often.

I hope this small script, or even the philosophy that inspired it, can be of some use to you.

👋 While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay