Making Bash Scripts

Bash scripts are very useful. They can do lots of different little jobs and some pretty important big jobs, but here’s a quick one that measures the speed of your internet connection.

Problem

I don’t know to make a bash script?

Solution

Making this bash script will quickly teach me.

Recipe

  1. To create and then run this bash script you will need to open your Terminal which on OS X is located in the /Applications/Utilities folder. It looks like the above picture, your terminal may look different if you are using Linux or Windows.

  2. Open up a text editor (I use Sublime Text, it’s brilliant) and copy the following text into your new document: #!/usr/bin/env bash This command measures the speed of your internet connection.
    Python needs to be installed, but generally comes as standard on most operating systems. curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -

  3. Save your new Bash script somewhere on your computer, in this case I’ve saved it to my Home folder. We next need to make this plain file an executable command we can run.

  4. Navigate to wherever you saved your script, in my case it was my home folder so I open up my Terminal and type the command cd ~/ NOTE: the tilde symbol represents the Home folder on most Operating Systems when you are in the terminal.

  5. Type the following command to make our speed test script executable: chmod +x speedtest.sh Our new script is now ready to run.

  6. To actually run the script and find out just how fast (or slow!) our internet connection is you will need to open your Terminal and type the command ./speedtest.sh and then press enter and away it will go! We use the leading ./ at the beginning of the command so the computer knows we want to run the script from this location. We could install this script and a special directory so that we would be able to type the command speedtest from anywhere on our machine and have the script run, but that’s outside the scope of this post. I will happily write up instructions if anyone wants them.

  7. And finally we have the results of our first bash script! See the picture, you did that, you measured your own internet speed using bash scripting. The terminal isn’t just for hackers who do bad things and is nothing to be frightened of. It’s incredibly useful.

  8. In conclusion, I can’t really overstate just how useful learning how to write simple bash scripts can be. I use them all the time and they make my life as a quadriplegic computer user 14.6% easier, if you find yourself doing something over and over that is repetitive and boring than the computer should be doing it and it’s time to get Bash scripting.

More