DEV Community


Posted on

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 :


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

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.

Top comments (0)