Hello, friends. In this post, you will learn How to run scripts without typing the full path? So, you can use these scripts as commands from a terminal or launcher.
Introduction
When making a bash script with some automation instructions, we have to run it from the terminal as follows
bash [script-path]
Or as follows
sh [script-path]
In this sense, we always have to locate the full path of this script to execute it. However, there is a way to make this script run as a system command.
This is so that you can use it from anywhere in the prompt, or even from system launchers.
How to run scripts without typing the full path
The solution is simple and is solved by creating a symbolic link to a directory that is in the PATH.
To find out what directories are in the PATH, run
echo $PATH
And you will get an output screen like this:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/bin:/usr/games:/usr/local/games:/snap/bin
Normally, you would make the symbolic link to any of these locations, with /usr/bin
or /usr/local/bin
being the most recommended options.
So, the procedure is easy thanks to the ln command.
sudo ln -s /full/path/to/your/shfile /usr/local/bin/name
Remember that name
will be how you want the command line to run. For example,
sudo ln -s ~/test.sh /usr/local/bin/test
The result will be that when you run
test
It will actually run the script ~/test.sh
.
In this simple way, you can run scripts without typing the full path.
Recommendation
A good practice is to create a specific directory for the scripts and then add it to the PATH.
This will avoid mixing scripts with binaries in the system directories. You will also have control over the scripts that will be in the PATH.
Conclusion
This short but useful post, helped you to make the bash scripts that you make, can quickly be added to the PATH, so you can run it without knowing the entire path.