Command Line Mastery: How to make executable scripts

An executable is a file or program that can be run by a computer. For example, the ls command.

You can find the location of any executable that you use by using the which command that will output the location. For example, try running:

which ls

My ls command was in /bin/ls. If you try to view the file itself however, you won’t be able to read it in this case, because it has been compiled.

What’s great about executables, is you can make your own! You’ve already seen aliases in this post, but that’s all bash stuff.

In this post, I will teach you how to make your own executables. Not just in bash but in the language you feel most comfortable using.


Some basics before we get started.

I am going to assume that if you are reading this, you have a basic knowledge of the terminal. I.e. navigation, creating files and folders etc. However, I am going to briefly explain the following:

  • File permissions
  • The chmod command
  • The PATH variable

File permissions

Let’s jump right into it. Right now open up your terminal and type in ls -lah

You will see output similar to the below.

ls -lah 
drwxr-xr-x   6 james  staff   192B  6 Apr 19:50 .
drwx------@ 47 james  staff   1.5K  6 Apr 19:50 ..
-rw-r--r--   1 james  staff     0B  6 Apr 19:50 image.jpg
-rw-r--r--   1 james  staff     0B  6 Apr 19:50 image.png
-rw-r--r--   1 james  staff     0B  6 Apr 19:50 index.html
-rw-r--r--   1 james  staff     0B  6 Apr 19:50 styles.css

For now, we are going to focus on the left hand side of the lines. I.e. -rw-r--r-- These are called the read write and execute bits. We care about the execute bit which for most files is set to -.

chmod command

The chmod command allows you to change these file permissions. Create a file called test.txt in your current directory. Now run ls -lah again. Note the permissions that it has.

Now we are going to make it executable. Run chmod +x test.txt the following and see what happens. Then run ls -lah again and see what’s changed.

chmod +x test.txt
ls -lah 

-rwxr-xr-x   1 james  staff     0B  6 Apr 19:52 test.txt
note: ---^

It now has an executable bit set to x.

PATH variable

PATH is an environment variable. It’s a list of all of the folders that bash checks for executable files.

To see your current PATH variable, run echo $PATH. You will see similar output to the below.


Note that each folder /directory is separated by a colon character.

We can add to this PATH variable if you have another folder full of executables you want to be able to run from anywhere.

How to make an executable that you can run anywhere

  • Create a bin directory on your PATH
  • Make a new file and set it to be executable
  • Getting input

Create a bin directory on your PATH

Create a folder called bin in your home directory. Bin is a common convention for keeping executable files in. Bin is short for binary.

Edit your bash profile. Search it to see if the PATH variable is already being set to something else.

You’ll see something like export PATH="something:$PATH". It’s critical that you always add :$PATH to the end to ensure that the other directories don’t get removed.

If you see something similar to the below, add our one it on the next line, otherwise, add it to the bottom.

export PATH="$HOME/bin:$PATH"

This says: set the path variable PATH to a bin directory in my home directory and append (add) the current PATH variable at the end.

Save your bash profile and then to update the terminal with the new PATH variable. To do this run source name-of-your-bash-profile, for example, source .bashrc .

Make a new file and set it to be executable

In the bin directory, create a file called bashHelloWorld. Inside add a shebang and an example hello world:

echo Hello World

A shebang is the start of an executable file. The #! part. It tells the terminal which language the script is in and which interpreter to use. In this case, the bash interpreter which is located in /bin/bash. If you were using python, this could be /usr/bin/python. This is where the which command comes in. I found where my python interpreter was by running which python.

Getting input

Getting input means being able to add text to change how the executable behaves. This is the same concept as parameters in function/method names. For example, you might want to specify a count for a loop to make something happen x amount of times.

I’ve prepared a very simple bash example which takes in the first parameter passed in and prints it out then again in lower and upper case.

echo $1

echo "UpperCase: $1" | tr '[:lower:]' '[:upper:]'
echo "LowerCase: $1" | tr '[:upper:]' '[:lower:]'

How I use executables

I create bin files any time I want to do something from the terminal that’s a bit more complex than an alias. Sometimes it’s when I want to leverage something that works really well already in a language that I’m familiar with.

The first executable file I created was to leverage the Zlib functions in PHP. I needed to unzip files that had been gzipped on a server in PHP to be able to see them locally. Shout out to Craig S for showing this one to me.

In work every few days there is a script that I run as a temporary workaround for an issue on an application. I specify a loop number as input and it runs a curl command x number of times to request a HTTP API endpoint. This is to save clicking a button in postman a bunch of times until we finish working on the fix.

I’ve also created several over the years for creating class files based on a template where only two or three things change each time. However, now I prefer to do this from my IDE.

Free executable code samples!

I imagine that you don’t normally write your code in bash. So, I’ve prepared a git repository of example executables in Java, Bash, Node (JavaScript), PHP, Python and Ruby!

I’ve given an explanation of how to run each of these and get input. They all print out the first parameter and then print it out again in lower and upper case.

If you want to use them I’m assuming that you already can code in that language and that you have all of the tools installed required to do so.

Feel free to fork the repository and submit a merge request with any languages that I’ve missed!

2 thoughts on “Command Line Mastery: How to make executable scripts

  1. Matthew Hamilton says:

    Hi James,

    This is a great post, really easy to follow! I love that you included a git repo of free scripts, I’m going to make use of the Java ones!

    I’m going to make an executable in Java (I know it better than bash) for generating XML files for a project in work. We currently have a UI for doing it but it’s a lot of hassle to use for all it’s doing. I’m going to make a template with a few of the repeated bits passed in as paramters to the script.

    I’ve a question, what’s the benefit of using an executable when you can just put all of that into an alias? ( I read your other post!)


  2. James Anderson says:

    Hi Matthew,

    Thanks for the feedback, I’m glad you enjoyed the post.

    That’s a great idea. I’ve made a similar one for generating JSON for one of my projects.

    From my point of view it’s kind of an opinion thing and kind of a practical thing.

    If I’m making something that’s more a few lines long I’ll make an executable file. Otherwise I’ll put it into an alias or a function in my bash profile.

    If you want to write any other language than bash, such as python you’ll need an executable. It also means you can store everything in a tidy git repo and you can’t accidentally delete parts that are near something else you’re deleting.

    I’ve found that if my bash profile gets too big with aliases and functions and themes it takes a long time to open and sometimes it takes longer between entering commands as more computation has to be done to show the prompt again.

Leave a Reply

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