Getting Started with Bash Scripting

“Create your Own Bash Scripts and Free Yourself from Code Drudgery.”

Once you’ve been using the terminal to move around your system for a little while, creating/deleting/moving files and the like, the next step is to start writing your own Bash scripts. And this is where the real joy of using Bash comes into play – being able to automate all the repetitive tasks you do in your daily web development with scripts you can call with a simple one liner in the terminal:

$ super-script.sh save-me-from-drudgery

So in this post I’m going to show you:

1) Where to store Bash scripts.
2) How to create a Bash script and get it up and running.

Then at the end I’ll give you a few simple Bash scripts that I use regularly and that you might also find helpful in your web development work. Let’s go.

Where to store Bash scripts?

First we need to create a folder to store our Bash scripts. The best place to put them is ~/bin/. By default this folder doesn’t exist, so let’s move to our home folder ~ and then create the folder bin:

$ cd && mkdir bin

The cd command, without any following destination, is a shortcut for change directory to home folder. The && just let’s us add a second command, which is to make a directory/folder named bin – mkdir bin.

So we have somewhere to put our scripts, now let’s tell our system where they are located. Remember computers are dumb, so just because your scripts are on the system, don’t assume the system knows where they are!

To do this we need to open one of our configuration files, ~/.bashprofile, and put in this line of code:

export PATH=$PATH:~/bin

This tells your system to keep an eye on your bin folder for any scripts to run. To make this stick, it’s a good idea to quit out of your terminal emulator and then reopen it. Alternatively you type $ source .bashprofile into the command prompt and this will load your new settings.

How to create a Bash script and get it up and running?

Right, we’ve set up a place for our scripts, now let’s create one, write some code and then run it.

$ cd ~/bin
$ touch test-script.sh

This creates test-script.sh in you bin script folder. The .sh extension is optional, but I use it as I like to use as many pointers to functionality in my coding as possible – the .sh extension immediately tells me I’m running a Bash script rather than something similar like a Bash alias.

Moving on, you can then use your favourite text editor (I use nvim btw) to add this code to test-script.sh:

#!/bin/bash
echo 'hello from new test-script.sh'

#!/bin/bash is boilerplate that tells the computer this script is a Bash script (again we have to tell the computer everything). And then echo 'hello from new test-scrip.sh' is just an echo command to test our script is working.

Don’t worry, almost there (as a motivating note we will be automating a lot of this setup later on, so hang in there). However, it’s pretty handy to have at least some idea of what’s going on before we go a further step, so let’s get through it, you can cuss me later.

Now we just need to run this last command:

$ chmod 755 test-script.sh

This changes the test-script.sh file permissions so that it will now be executable from anywhere on your computer. Now you can be anywhere on your system and type:

$ test-script.sh

And the script will run, echoing hello from new test-script.sh into your terminal.

So we’ve set up a place to store our scripts, we’ve learnt how to create a Bash script, we’ve also set the correct file permissions, and finally run our new script.

One line to Setup a new Bash Script? Count me in!

This is my new-script.sh, which as it implies, sets up a Bash script quickly and easily, basically removing all the faffing we have just done previously:

#!/bin/bash

touch ~/bin/"$1".sh
echo '#!/bin/bash' >> ~/bin/"$1".sh
chmod 755 ~/bin/"$1".sh
echo "hello from $1.sh"
sleep 1
nvim ~/bin/"$1".sh

Save this code inside ~/bin/new-script.sh and then from anywhere on your computer you can type $ new-script.sh chicken into the terminal and it will:
– create a script called chicken.sh in your bin folder. It uses the $1Bash variable so that whatever your second word is after calling new-script.sh will become the name of the new script to create.
– Give this new chicken.sh script the correct file permissions.
– Echo “hello from chicken.sh” into the terminal, which is just quick way to check our new script is clucking like a chicken.
– Waits 1 second, just to give the whole process some space/air, and then opens my text editor, nvim. You can of course change this to Nano or VS Code (boo!) or Emacs (are you mad?) or whatever is your poison..

From there you can begin composing your new Bash script, knowing all the boilerplate setup is already done.
Ahh Lovely.

Another script? Oh come on then. This one is a bit more involved but Oh such a time saver.

Setup a local Git Repo and then Backup to your Github in one line? Yes please!

All you need for this one is your personal access token from Github. Then make a folder, do some work, type git-repo.sh name-of-your-new-repo on the command line and this Bash script does the rest:

#!/bin/bash

# You need to cd into the directory before running this with `git-repo.sh name`

# This is the github "private access token"
token=put-your-github-personal-access-token-here

# check repo name hasn't been left out
if [ -z "$1" ];
then
    echo 'you forgot to include a name for the new repo'

else
    # create local repo
    git init
    cp ~/coding-files/coding-notes/.gitignore .
    git add .
    git commit -m 'Initial commit of project'

    # create remote repo
    curl -u michael-odonovan:$token https://api.github.com/user/repos -d "{"name":"$1","private":"true"}"

    # link up remote repo to origin/local repo
    git remote add origin https://github.com/michael-odonovan/$1.git

    # rename master branch to main
    git branch -m master main
    git push -u origin main
fi

Note you have to run it from the folder that will become the Git repo. And you will of course have to change your name from michael-odonovan, unless of course that is your name, which is just too much to think about for today.

So there we go, we set up a place to put our Bash scripts, learnt some basic Bash script functionality, and then grabbed a couple of real world Bash scripts to automate the setup of all future Bash scripts and Git repos.

Not bad, you.

Have fun, if you get stuck, ask in the comments and I shall try and solve.

Mike

Leave a Reply

Your email address will not be published. Required fields are marked *