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.

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:

Add Directories To Path Bash Paths

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

Add Directories To Path Source Bash

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

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:

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:

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:

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:

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.

5 comments

  1. 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

    1. 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.

    2. 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.

  2. 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.

  3. 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!

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.