how to make shell script executable from anywhere in linux

In Linux, shell scripts are the standard staple of everyday life. Most programs or software in Linux and Unix use some script file to start or run. It is roughly equivalent to executable files in Windows. You will also be able to create custom shell scripts to automate many of the everyday tasks as well.

It is not uncommon to end up with several custom scripts that you will need to execute repeatedly. Most times it is a hassle to have type in the absolute path to the script or having to dig around as to where the file is. It makes it much easier to just type in the shell script name from any folder and execute it.

A word of caution here, you will have to make sure that the shell script is written with the consideration that it can be executed from any directory. It should not make any assumption about the executing folder. If there are relative folders referred with in the commands with in the script, then you might be in for a surprise, but you already know that.

The first step is to make the script executable and make sure that it has the appropriate permissions for the users executing it. You will need to set the executable bit on the file for the user groups.

You also need to be aware of the path variable of the executing shell. In Linux environments, it is denoted by the PATH. You can view your current path by using the following command:

bash$ echo $PATH

Remember that if there are two scripts by the same name, then the first script found in the path will take precedence. The only way to execute the latter script will be to use the absolute path to the script.

Shell Script in the PATH

Now in order to make it executable from anywhere, you have two options:

  1. Copy the script into a folder that is already in the path: This option is most helpful if do not want to change the existing path variable. This is also a good way to keep your scripts all in one folder, which makes it easier to maintain on the long run.
  2. Add the path to the shell script to the path: You can add the path or the folder where the script is located into your path. This is quite useful if you cannot move script for some reason such as when the script is part of an installation of some third party software. But adding each and every folder where scripts exist can make the path pretty long and harder to maintain.

You can follow some kind of standard process in order to keep it manageable and maintainable. I usually use two different predefined path or folders for scripts: /opt/bin/ and ~/bin/.

The /opt/bin/ folder gets all the custom scripts that needs to be shared by different users or there are multiple user accounts that needs to execute it. This also hold the scripts that are part of a package or software installation.

The ~/bin/ gets all the custom scripts that is local to me, and i am the only user executing it. These are usually custom scripts that perform a specific task such as searching or copying files etc etc.

However, there is a small issue when you install packages or custom software. The startup scripts for these software exists in the location where the software is installed. If you installed them using a standard package manager specific to your distro, then appropriate symbolic links are created automatically in pre-existing path such as /usr/bin/. Otherwise, you will need to execute the script directly from the installation folder.

Using symlinks or symbolic links

Let’s say that you have installed multiple versions of Mozilla Firefox and you need to start different versions at different times. There are two issues here: for one they all have the same startup script name and the installation folders are not consistent and can be anywhere.

The symbolic links or symlinks can be quite handy here. You can create symbolic links in a specific folder that point to the original script (for example in /opt/bin/) that is already in the path. This keeps references to the scripts all in one folder (/opt/bin) and also allows you to specify different names for each of them.

bash$ ln -sfn /opt/latest/firefox-46.0/firefox /opt/bin/firefox-46
bash$ ln -sfn /opt/nightly-software/firefox-nightly/firefox /opt/bin/firefox-nightly
bash$ ln -sfn /opt/firefox-44.0/firefox /opt/bin/firefox-44

You can now use commands firefox-46, firefox-nightly or firefox-44 to start the specific version that want to run. There is no need to know exactly where the installation is and also you have renamed the script to something that is easily distinguishable.