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
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.
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:
For example, if you keep your executable scripts in folder “/home/myname/scripts”, the command would be:
To register the changes, save the file, exit the text editor and then type in your terminal:
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:
You have to logout and re-login to enable the changes.
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:
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.
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:
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:
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.
- How to Get Commands Explanation in Terminal with Kmdr
- An Even Better Way to Search Your Command Line History
- Basic Bash Commands for Linux Newbies
Our latest tutorials delivered straight to your inbox