
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. Bash scripts are typically available and run only in their 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.
Create a Script File
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.console$ nano greetings.sh
Add the following code to the file.
bash#!/bin/bash echo "Welcome to Vultr" echo "This script is running from: $(pwd) and is executed at: $(date)"
Save and close the file. This script does the following:
- Uses the Shebang directive
#!/bin/bash
to indicate that the script should be run using the bash interpreter. - Print
Welcome to Vultr
to the terminal when the script runs. - Print the working directory using
$(pwd)
and the date and time$(date)
at the moment the script is run.
- Uses the Shebang directive
Make the Script Executable
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.
console$ ls -l greetings.sh
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:
-rw-rw-r-- 1 linuxuser linuxuser 107 Jul 25 02:52 greetings.sh
The absence of
x
(execute) means the file is not executable.Set execute permissions for the script.
console$ chmod +x greetings.sh
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.
console$ ls -l greetings.sh
Output:
-rwxrwxr-x 1 linuxuser linuxuser 107 Jul 25 02:52 greetings.sh
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.
Run the Bash Script
Like any language, a bash script can be interpreted and executed without being explicitly made executable, which is a common practice. 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.console$ bash greetings.sh
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.console$ sh greetings.sh
This runs the script using your system’s default shell interpreter.
Run the script with the
source
command.console$ source greetings.sh
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.Notesource
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, thebash
andsh
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.console$ ./greetings.sh
Output:
Welcome to Vultr This script is running from: /home/user and is executed at: Wed Jun 18 09:35:20 PM WAT 2025
Notechmod +x
command, this would not work.Run the bash script from any directory.
Make a test directory
test
.console$ mkdir test
Switch to the directory and run the script.
console$ cd test/ && ./greetings.sh
Output:
-bash: ./greetings.sh: No such file or directory
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.
Make Script Executable from All Directories
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.
console$ echo $PATH
Output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
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.
console$ cd
Move your bash script to
/usr/local/bin
.console$ sudo mv greetings.sh /usr/local/bin/
Verify that your script is globally accessible.
console$ greetings.sh
Output:
Welcome to Vultr This script is running from: /home/user and is executed at: Sun Jun 22 07:27:12 PM UTC 2025
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.
It is possible to make your scripts globally accessible for your user account by adding them to the ~/.local/bin
directory. You can store personal scripts and executables specific to your user without using the sudo
command. In the steps below, create a ~/.local/bin
directory, move your bash script into it, and add it to your PATH environment variable to make it globally accessible for your user account only.
Switch to your home directory.
console$ cd
Create the
~/.local/bin
directory.console$ mkdir -p ~/.local/bin
Move your script to the
~/.local/bin
.console$ mv greetings.sh ~/.local/bin/
Check if it is in your PATH.
console$ echo $PATH | grep -o "$HOME/.local/bin"
If the command returns no output, the
~/.local/bin
directory is not included in your PATH. You’ll need to add it manually to your shell configuration file.Add the
~/.local/bin
directory to your shell configuration file -~/.bashrc
.console$ echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
Reload your shell configuration file for the change to take effect.
console$ source ~/.bashrc
Check that the
~/.local/bin
directory is now added to your PATH.console$ echo $PATH | grep -o "$HOME/.local/bin"
Run your script from any directory. For example, the
~/test2
directory.console$ mkdir -p ~/test2 && cd ~/test2 && greetings.sh
Output:
Welcome to Vultr This script is running from: /home/user and is executed at: Sun Jun 22 07:27:12 PM UTC 2025
Remove a Bash Script From Path Directories
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/
.console$ sudo rm /usr/local/bin/greetings.sh
Remove the script from
~/.local/bin/
.console$ rm ~/.local/bin/greetings.sh
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.console$ greetings.sh
Output:
-bash: /home/linuxuser/.local/bin/greetings.sh: No such file or directory
The script will now only be accessible from your home directory (the original directory) using
./greetings.sh
.
Conclusion
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; either /usr/local/bin/
for all users, or ~/.local/bin/
for your specific user account.
No comments yet.