How to Make Your Scripts Executable Everywhere in Linux
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 ls
, imagemagick
, apache
with 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:
export PATH="/path_of/the_folder_we/want_to_add_to:$PATH"
For example, if you saved the executable scripts in the “/ home / myname / scripts” folder, the command would be:
export PATH="/home/myname/scripts:$PATH"
To register the changes, save the file, exit the text editor, and enter in the terminal:
source ~/.bashrc
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:
export PATH="$PATH:$HOME/scripts"
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.
Type in:
PATH DEFAULT=${PATH}:/home/@{PAM_USER}/scripts
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:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin"
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:
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 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.
related:
- 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
Ranch
Ranch