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
- 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?
May be interested
- 10 AutoHotKey scripts to make life easierwith autohotkey (version 2), you can recreate these scripts and bind them to keyboard shortcuts, making them feel like built-in windows features that microsoft forgot to mention.
- 5 ways to make Linux desktops look greatvarious tools, tips and tweaks can be used to personalize linux desktops. use the following 5 methods to personalize your linux desktop environment.
- Techniques cited in Unix / Linuxshell provides a variety of characters that have special meaning while using them in any shell script and cause a limit of a word unless quoted.
- How to Enable and Use Script Execution Policy in Windows PowerShellby default, script execution on powershell is disabled as a security measure to prevent malicious scripts from running on the system. the following article will show you two ways to enable script execution policy in windows powershell.
- How to create and run a shell script in Ubuntu 20.04 LTSshell is the command interpreter written by the user. script shell helps users write and execute multiple commands at the same time. in this article, you will learn how to execute shell scripts through command line input.
- Basic Linux commands everyone needs to knowwhen using linux, you should also learn basic linux commands to make operations quicker and easier. here are some basic commands on linux you should know.
- How to create and run a PowerShell script file on Windows 10a script is a collection of commands stored in a text file (using the special extension .ps1) that powershell understands and executes step by step to trigger a variety of operations.
- Making Linux look like macOS with these easy tweaks!you can learn the simple steps needed to make the linux desktop work and look like apple's macos.
- Difference between Zsh and Bashboth shells provide powerful scripting languages but they are not fully compatible with each other. let's find out the difference between zsh and bash with tipsmake.com.com!
- Why don't developers make more apps for Linux?linux has a lot of software, but when you first switch, you may be disappointed that the operating system lacks familiar names. if you've been using linux for a while, you may also be disillusioned by certain types of software that have a relatively slow rate of change or lack of long-term support.