How to Make Your Scripts Executable Everywhere in Linux

Add Directories To Path Featured

When you created a Bash script and save it in a folder, you will find that you can only execute it when you are in that folder. Have you ever notice how ls, imagemagick, apache, and squid might be installed in different directories but accessible everywhere? That’s because their individual paths have been added to the “Path” variable. By adding more paths to it, you can make your scripts executable everywhere too.

Tip: Check out our regular expressions cheatsheet.

Adding paths to Bash

Before we begin, we should explain that thanks to how Linux security works, you can tweak the Path on three different levels. Bash is the first of them. Everything we see here will affect Bash, and everything that runs in it, but have no effect “outside Bash.”

Let’s say you have a collection of scripts in a folder you want accessible from everywhere.

Add Directories To Path Scripts Folder

To pull this off, you can add their path to “~/.bashrc”. You can open up the “.bashrc” file (it is in your Home directory, but is hidden by default) in your favorite text editor, like gedit.

Go to the very end of the file and add:

export PATH="/path_of/the_folder_we/want_to_add_to:$PATH"
Add Directories To Path Bash Paths

For example, if you keep your executable scripts in folder “/home/myname/scripts”, the command would be:

export PATH="/home/myname/scripts:$PATH"
Add Directories To Path Source Bash

To register the changes, save the file, exit the text editor and then type in your terminal:

source ~/.bashrc
Add Directories To Path Test Script

After that, move to different directories and try to run your scripts from there.

Adding paths to your profile

If you want the content of your folder accessible from outside the constraints of Bash, add it to the Profile variable instead.

Open the “.profile” file with your favorite text editor.

At the very end of the file, enter:

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

You have to logout and re-login to enable the changes.

Add Directories To Path Edit Pam

In Ubuntu and its derivatives, it’s suggested you edit the “.pam environment” file instead of “.profile”.

Open the “.pam_environment” file in the text editor. If the file doesn’t exist, create it.

In it, enter:

PATH DEFAULT=${PATH}:/home/@{PAM_USER}/scripts
Add Directories To Path Pam Paths

Note that instead of a fully hardcoded path, and unlike in the profile file, here we use a variable. This way, each user’s “/home/USER_NAME/scripts” folder would be added to their path.

As when editing the “.profile” file, you have to log out and re-login for the changes to take effect.

Add Directories To Path Log Out

Adding paths to the environment

The proper way to have the contents of a folder accessible from multiple users who share the same computer is by adding it to the environment path variable. Fire up a terminal and enter:

sudo nano /etc/environment

The path variable there contains a bunch of folders in quotation marks, split by colons, similar to:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin"

To include your own folder in that list, right after the last path, before the closing quotation mark, enter a colon and the path to your folder. If your folder was, again, “/home/your_username/scripts,” it should look like this:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/YOUR_USERNAME/scripts"
Add Directories To Path Environment Paths

Note that it doesn’t have to be in caps – we used them for emphasis, to help identify where and how you should include your folder.

As before, log out and re-login to apply the changes.

With the above tricks, you will be able to run your scripts from anywhere in Linux.

Related:

Odysseas Kourafalos
Odysseas Kourafalos

OK's real life started at around 10, when he got his first computer - a Commodore 128. Since then, he's been melting keycaps by typing 24/7, trying to spread The Word Of Tech to anyone interested enough to listen. Or, rather, read.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox