An executable is a file or program that can be run by a computer. For example, the
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:
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.
I’M GIVING AWAY FREE EXECUTABLE CODE SAMPLES AT THE BOTTOM OF THIS POST!
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
Let’s jump right into it. Right now open up your terminal and type in
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 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 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.
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:
#!/bin/bash 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
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.
#!/bin/bash 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’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!