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.
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:
export PATH="/path_of/the_folder_we/want_to_add_to:$PATH"

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

To register the changes, save the file, exit the text editor and then type in your terminal:
source ~/.bashrc

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.

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

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:
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"

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:
- 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
5 comments
Comments are closed.
Hi
Topic is interesting But – In my “ubuntu” home we don’t have .bashrc, same nothing in MX.
Could you clarify please?
I made already some .sh and have show path when execute.
Thanks
That’s probably because it’s hidden by default. Either enable the display of hidden files in your file manager of choice, or try typing the name of your favorite text editor (like nano, vi, gedit etc.) in a shell, followed by “~/.bashrc” (without the quotation marks). Hit enter and the file will (probably?!) open in it. AFAIK, .bashrc can be found at the same spot in most distributions.
You can use your Find Files/Folders utility from your Application menu.
In the Named: field type in *bash* (with the asterisks, they are wildcards)
In the Look In: field type in /
Check off the Include Subfolders box
Leave other check boxes unchecked
Hit Find
After the search is finished, you will see displayed all the file/folders names with the characters “bash” in them. You should sort the display on the Name field. Then scroll though the display looking for “bashrc”. Bash configuration files are of “Plain Text” type. If you do not find “bashrc”, it may be stored under a slightly different name. Open all text files in the listing, one by one, until you find what you are looking for.
I knew about hidden, in Mint I found dot.bashrc in /usr/share/base-files and .bashrc in /etc/skel
both out of home and compare to Debian distro first one looks similar.
I know about adding to the path after installing a new script or command, but there are some that I installed some months ago, have used then a few times or never used them and have forgotten their name or even their existence. It would be extremely useful to have a script which ‘finds’ these commands and lists them. Something like the bauh gui that lists your snap and flatpack programs. Yes, I am an elderly user, and I may be losing it, but it would help others who are beginners!