When you create a Bash script and save it in a folder, you will find that the script can only be executed in that folder. Did you notice
squid May be installed in a different directory but accessible everywhere? That’s because their respective paths have been added to the “Path” variable. By adding more paths, you can also make the script executable anywhere.
Add path to Bash
Before we start, we should explain how you can adjust the Path at three different levels due to how Linux security works. Bash is the first. Everything we see here will affect Bash and everything running in it, but has no effect “outside Bash”.
Suppose you have a set of scripts in a folder you want to access from anywhere.
To do this, you can add its path to “~ / .bashrc”. You can open the “.bashrc” file in your favorite text editor (such as gedit) (located in your home directory, but hidden by default).
Go to the end of the file and add:
For example, if you saved the executable scripts in the “/ home / myname / scripts” folder, the command would be:
To register the changes, save the file, exit the text editor, and enter in the terminal:
After that, move to another directory and try to run the script from there.
Add a path to your profile
If you want to access the contents of the folder from outside the Bash constraint, add it to the Profile variable instead.
Open the “.profile” file with your favorite text editor.
At the end of the file, enter:
You must log out and log in again to enable changes.
In Ubuntu and its derivatives, it is recommended that you edit the “.pam environment” file instead of the “.profile” file.
Open the “.pam_environment” file in a text editor. If the file does not exist, create it.
Note that variables are used here instead of fully hard-coded paths, and are different from configuration files. This way, each user’s “/ home / USER_NAME / scripts” folder will be added to their path.
As when editing the “.profile” file, you must log out and log back in for the changes to take effect.
Add environment path
The correct way to make the contents of a folder accessible to multiple users sharing the same computer is to add it to the environment path variable. Launch the terminal and enter:
sudo nano /etc/environment
The path variable there contains a bunch of folders enclosed in quotes, separated by colons, similar to:
To include your own folder in the list, after the last path, before the right quote, enter a colon and the path to the folder. If your folder is still “/ home / your_username / scripts”, it should look like this:
Note that it doesn’t have to be capitalized-we use them for emphasis to help determine where and how to include folders.
As before, log out and log back in to apply the changes.
With the above tips, you will be able to run scripts anywhere in Linux.
- How to use Kmdr to get command instructions in the terminal
- A better way to search the command line history
- Basic Bash commands for Linux newbies