
A bash script allows you to run a sequence of commands in one go, which is helpful when running the same set of commands on multiple occasions. After a bash script is written, it can be shared and reused on different systems, saving time and reducing manual work. By default, bash scripts are executed from the script's directory.
This article explains how to run bash scripts from any location in Linux. You will create and run a bash script, make it executable and globally accessible, and understand the different methods for achieving this.
Before making a bash script globally accessible, you need to create a working bash script. In the following steps, create a script you will use throughout this article.
Create and edit a greetings.sh file in your home directory.
Add the following code to the file.
Save and close the file. This script does the following:
#!/bin/bash to indicate that the script should be run using the bash interpreter.Welcome to Vultr to the terminal when the script runs.$(pwd) and the date and time $(date) at the moment the script is run.By default, newly created files may not have execution permissions. To make a bash script executable, you must modify its permissions to run it directly from your terminal like any other command-line program.
Verify the script's file permissions.
From the command above:
ls: Lists all information about the file.-l: Shows detailed information (permissions, ownership, size, and timestamps) about the file.greetings.sh: Represents the file you want to examine.Output:
The absence of x (execute) means the file is not executable.
Set execute permissions for the script.
The command:
chmod: Command to modify the permissions of files and directories in Linux.+x: Grants execute permission to the file owner, group, and others.greetings.sh: Represents the target file.Verify the file permissions again.
Output:
The target file greetings.sh in the output should turn green or a different color depending on your terminal’s color theme to indicate it's now an executable file and can be run from your terminal.
A bash script can be executed without explicitly setting execute permissions by calling it through an interpreter. You can run a bash script using the bash, sh, or source commands as long as you run the script from the directory it was created in and is readable by the user attempting to execute it. You can also run the bash script using the complete path to the script without setting execute permissions. In the steps below, run the bash script using bash, sh, and source.
Run the bash script with the bash command.
This runs the script by calling the bash interpreter. The file doesn’t need to be executable to run.
Run the script with the sh command.
This runs the script using your system’s default shell interpreter.
Run the script with the source command.
The source command is usually used to run a script in your active shell and make the changes made by the script persist in your active shell session. In this case, it is used to execute the commands inside greetings.sh within your active shell environment rather than starting a new subshell.
Use the source command when a script needs to make changes to your active shell session, for example, exporting environment variables, defining shell functions or aliases, and so on. Otherwise, the bash and sh commands are enough for performing tasks such as backing up files, installing packages, and printing messages. Something that doesn’t affect the active shell session.
Run the script using the ./ prefix.
Output:
Without making the script executable using the chmod +x command, this would not work.
Run the bash script from any directory.
Make a test directory test.
Switch to the directory and run the script.
Output:
Whether you run a bash script using bash, sh, source, or ./ if it’s not executed within the directory where the bash script is, it will fail with a “No such file or directory” error. Unless you enter the absolute (full) path to the script or add the script’s directory to your PATH environment.
To conveniently access a bash script from anywhere on your system, you must place it in a directory that is already part of your system’s PATH environment variable. Within the PATH environment variable, your shell searches for the file when you type a command. Any script in a PATH directory automatically transforms from a local script that only works in its creation directory into a system-wide command/utility that can be executed from any location on your filesystem.
In the steps below, view your PATH environment variable and move your bash script to /usr/local/bin/.
View your PATH environment variable.
Output:
The output above shows the list of directories in the PATH environment variable. These directories contain executables and binaries that can be run from anywhere in the system.
Navigate back to your home directory.
Move your bash script to /usr/local/bin.
Verify that your script is globally accessible.
Output:
The /usr/local/bin directory includes user-installed programs and scripts; it is already included in your PATH and thus doesn’t interfere with system-managed packages. However, sudo is required to move your files to the /usr/local/bin directory, as it is a system directory. In addition, all users can access and use scripts here.
If your bash script is no longer needed, you can remove it from your PATH directories if you have added it there. In the following steps, remove the bash script - greetings.sh from /usr/local/bin/ and ~/.local/bin/.
Remove from /usr/local/bin/.
Remove the script from ~/.local/bin/.
You own this directory; therefore, sudo is not required.
Verify that the bash script is no longer globally accessible. For example, from the ~/test2 directory.
Output:
The script will now only be accessible from your home directory (the original directory) using ./greetings.sh.
In this article, you have learned how to create a bash script and run it in different ways. You learned how to make bash scripts globally accessible by placing them in PATH directories: /usr/local/bin/ for all users or ~/.local/bin/ for your specific user account.
0 Comments
Be the first to comment and share your perspective with the community.