Creating Bash Scripts

Let’s start with the ability to create scripts. With scripts, you can run sequences of commands. They prevent you from repeating yourself and allow you to store long and tedious to type commands for later usage.

A command-line script starts with the shebang — a sequence of the two characters #!. After these two characters, you can name any interpreter and a bunch of arguments, like this:

#!interpreter [optional arguments]

A shell script is usually based on the most popular shell called Bash, so this would simply become:


Sometimes you’ll see this instead:

#!/usr/bin/env bash

When using /usr/bin/env, the provided command will be searched for in the current $PATH, instead of hardcoding it to the /bin/bash location. Bash will almost always be found at /bin/bash, so the first method is fine for us.

However, you may want to use this trick to create an executable Python script. Python can be installed anywhere, depending on the way it is installed. I’ve seen it in /usr/bin//bin, and /usr/local/bin, just to name a few. If you’re running a virtual environment, it can be anywhere. So searching the $PATH is a good option, since that will find the Python version from the virtual environment.

We’ll get to creating an actual shell script in a moment. But first, we’ll take a look at variables and for-loops to juice things up a little.

About Erik van Baaren

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python! Writing good articles takes time and effort. Did you like this tutorial? You can buy him a coffee to show your appreciation.

Leave a Comment