How to make the script executable anywhere in Linux
When you create a Bash script and save it to a directory, you'll find that you can only execute the script while in that directory. Have you ever wondered how ls
, imagemagick
, apache
and squid
can be installed in different directories but still accessible everywhere? That's because their own paths have been added to the 'Path' variable. By adding multiple links to it, you can also make your script executable everywhere.
Add the path to Bash
You can adjust the Path according to 3 different levels. Bash is the first level. Everything we see here will affect Bash. Everything runs in there, but has no effect outside of Bash.
Suppose you have a collection of scripts in the directory you want to access from anywhere.
To do this, you can add their paths to '~ / .bashrc' . You can open the .bashrc file (it is located in the Home folder, but is hidden by default) in your favorite text editor, like gedit .
Go to the end of the file and add:
PATH="/path_of/the_folder_we/want_to_add_to:$PATH"
For example, if you keep your executable scripts in the '/ home / myname / scripts' directory , the command would be:
export PATH="/home/myname/scripts:$PATH"
To save the changes, save the file, exit the text editor, and enter this command in the terminal:
source ~/.bashrc
Then, move to many different directories and try to run scripts from there.
Add a link to the Profile
If you want the contents of the directory to be accessible from outside the Bash constraint, instead add it to the Profile variable.
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 back in to apply changes.
In Ubuntu and its derivative, you can edit the file '.pam environment' instead of '.profile' .
Open the file '.pam_environment' in the text editor. If the file does not exist, create it.
In the file, enter:
PATH DEFAULT=${PATH}:/home/@{PAM_USER}/scripts
Note that instead of the path being completely hardcode and unlike in the profile file, here we will use a variable. This way, each user's '/ home / USER_NAME / scripts' directory will be added to the Path.
As with editing the .profile file, you must log out and log back in for the changes to take effect.
Add the path to the environment
An appropriate way to access the contents of a directory from multiple users, sharing the same computer, is to add it to an environment variable. Open a terminal and enter:
sudo nano /etc/environment
The Path variable there contains a series of directories in quotation marks, separated by colons, similar to:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin"
To include your own directory in the list, right after the final path, before closing the quotes, enter a colon and the path to the directory. If your directory is '/ home / your_username / scripts' , it will 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"
Please log out and log back in to apply the changes.
With the above tips, you will be able to run your scripts from anywhere in Linux.
You should read it
- What is a shell?
- 12 scripts for AutoHotKey make life easier
- How to automate Photoshop with Photoshop Scripts
- How to run scripts on boot on the Raspberry Pi
- 5 example bash scripts to help you learn Linux programming
- How to execute .run or .bin packages in Linux
- Set up automatic mode in Linux with Crontab
- How to create and run shell scripts in Ubuntu 22.04
- 8 easy tools to create your own Linux Distro
- What is a web script?
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 10
- Office Scripts with the ability to automate routine tasks in Excel is now available on the web
Maybe you are interested
Why is there no professional mode on Facebook?
6 ways to fix the error 'PowerPoint found a problem with the content'
The Dark Web is not illegal, but be wary of these 7 things!
YouTube is about to support a feature that allows creators to reply to viewers' comments with their voice
9 ways to fix the error of not being able to save files as JPEG or PNG in Photoshop
Apple's 'shot on iPhone' ads don't reflect the truth