How Bash Scripts Work and Why They’re Useful

A bash script lets you execute a chain of commands in a single run, which is handy when you need to repeat the same command set more than once. Once you’ve written a bash script, you can share it and reuse it across different machines, which saves time and cuts down on manual effort. By default, bash scripts run from the directory where the script is located.

This article shows you how to execute bash scripts from anywhere on a Linux system. You will write and run a bash script, give it executable rights and global reach, and learn the various ways to make that happen.

Create a Script File

Before you can make a bash script accessible system-wide, you first need a working script. In the steps below, you’ll build the script used throughout this article.

Create and edit a greetings.sh file inside your home directory.

console

Copy

Explain Code

BETA

Add the following code to the file.

bash

Copy

#!/bin/bash
echo "Welcome to centron"
echo "This script is running from: $(pwd) and is executed at: $(date)"

Explain Code

BETA

Save and close the file. This script performs the following tasks:

  • It uses the shebang line #!/bin/bash to specify that bash should interpret the script.
  • It prints Welcome to centron in the terminal when the script runs.
  • It displays the current working directory with $(pwd) and the exact execution time using $(date).

Make the Script Executable

Newly created files often don’t include execute rights. To run a bash script directly like a normal command-line tool, you must adjust its permissions.

Check the script’s current permissions.

console

Copy

Explain Code

BETA

From the command above:

  • ls: Shows information about the file.
  • -l: Outputs detailed details such as permissions, owner, size, and timestamps.
  • greetings.sh: The file you want to inspect.

Output:

-rw-rw-r– 1 linuxuser linuxuser 107 Jul 25 02:52 greetings.sh

Because there is no x flag, the file is not executable.

Add execute rights to the script.

console

Copy

Explain Code

BETA

The command:

  • chmod: Changes permissions for files or directories on Linux.
  • +x: Gives execute permission to the owner, group, and others.
  • greetings.sh: The file being updated.

Check the permissions once more.

console

Copy

Explain Code

BETA

Output:

-rwxrwxr-x 1 linuxuser linuxuser 107 Jul 25 02:52 greetings.sh

In your terminal, greetings.sh should now appear in green (or another highlight color, depending on your theme). That indicates it is executable and can be launched directly.

Run the Bash Script

You can run a bash script without granting execute rights by invoking it through an interpreter. As long as you are in the directory where the script exists and you have read access, you may use bash, sh, or source. You can also execute it with its full path even if it is not executable. In the steps below, you will start the script using bash, sh, and source.

Run the script with the bash command.

console

Copy

Explain Code

BETA

This executes the script by explicitly calling bash, so the file does not need execute permission.

Run the script with sh.

console

Copy

Explain Code

BETA

This runs the script using your system’s default shell.

Run the script with source.

console

Copy

Explain Code

BETA

The source command is generally used to run a script inside your current shell so that changes made by the script remain active after it finishes. Here, it runs the commands in greetings.sh directly in your ongoing shell session instead of launching a new subshell.

Note

Use source when a script must modify your active shell environment, such as exporting environment variables or creating functions or aliases. For tasks like backups, installations, or printing output that do not affect the active shell, using bash or sh is sufficient. Something that doesn’t impact the active shell session.

Run the script using the ./ prefix.

console

Copy

Explain Code

BETA

Output:

Welcome to centron
This script is running from: /home/user and is executed at: Mo Nov 17 08:03:00 PM WAT 2025

Note

If you had not made the script executable with chmod +x, this method would fail.

Run the bash script from any directory.

Create a test directory named test.

console

Copy

Explain Code

BETA

Move into that directory and try running the script.

console

Copy

$ cd test/ && ./greetings.sh

Explain Code

BETA

Output:

-bash: ./greetings.sh: No such file or directory

If you try to run a bash script using bash, sh, source, or ./ outside of the script’s own directory, it will fail with a “No such file or directory” message. The only exceptions are when you provide the absolute path to the script or when the script’s folder is added to your PATH environment variable.

Make the Script Executable from Any Directory

To use a bash script from anywhere on your Linux machine, you need to store it inside a folder that is already listed in your system’s PATH variable. Your shell checks each PATH directory when you enter a command. When a script lives in one of these directories, it stops being a file that only runs in its original folder and effectively becomes a global command you can call from any location on your filesystem.

In the steps below, you’ll display your current PATH value and then move your bash script into /usr/local/bin/.

Display your PATH environment variable.

console

Copy

Explain Code

BETA

Output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

The output above is the collection of folders included in your PATH. Anything executable in these locations can be launched from anywhere on the system.

Go back to your home directory.

console

Copy

Explain Code

BETA

Move your bash script into /usr/local/bin.

console

Copy

$ sudo mv greetings.sh /usr/local/bin/

Explain Code

BETA

Confirm that the script can now be run globally.

console

Copy

Explain Code

BETA

Output:

Welcome to centron
This script is running from: /home/user and is executed at: Fri Nov 21 09:43:30 PM UTC 2025

The directory /usr/local/bin is intended for user-added tools and scripts and is already part of PATH, so it won’t conflict with system-managed software. Because it’s a system folder, you need sudo to move files there. Once placed inside, any user on the machine can run the script.

You can also make scripts available only for your own account by putting them in ~/.local/bin. This location is for personal executables, so you can manage scripts there without using sudo. In the steps below, you’ll create ~/.local/bin, move your script into it, and then add that folder to your PATH so only your user can run it globally.

Switch to your home folder.

console

Copy

Explain Code

BETA

Create the ~/.local/bin directory.

console

Copy

Explain Code

BETA

Move your script into ~/.local/bin.

console

Copy

$ mv greetings.sh ~/.local/bin/

Explain Code

BETA

See whether that folder is already in your PATH.

console

Copy

$ echo $PATH | grep -o "$HOME/.local/bin"

Explain Code

BETA

If you see no output, then ~/.local/bin is not part of your PATH yet, and you must add it manually in your shell config.

Add ~/.local/bin to your shell configuration file at ~/.bashrc.

console

Copy

$ echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc

Explain Code

BETA

Reload your shell settings so the update applies right away.

console

Copy

Explain Code

BETA

Verify that ~/.local/bin is now included in your PATH.

console

Copy

$ echo $PATH | grep -o "$HOME/.local/bin"

Explain Code

BETA

Run the script from any folder, for example inside ~/test2.

console

Copy

$ mkdir -p ~/test2 && cd ~/test2 && greetings.sh

Explain Code

BETA

Output:

Welcome to centron
This script is running from: /home/user and is executed at: Sun Nov 23 07:27:12 PM UTC 2025

Remove a Bash Script From Path Directories

If you no longer want the script available globally, you can delete it from any PATH directory you placed it in. In the steps below, remove greetings.sh from both /usr/local/bin/ and ~/.local/bin/.

Remove it from /usr/local/bin/.

console

Copy

$ sudo rm /usr/local/bin/greetings.sh

Explain Code

BETA

Remove the script from ~/.local/bin/.

console

Copy

$ rm ~/.local/bin/greetings.sh

Explain Code

BETA

Because this folder belongs to your user, you don’t need sudo to delete the file.

Confirm that the script is no longer globally available, for example from ~/test2.

console

Copy

Explain Code

BETA

Output:

-bash: /home/linuxuser/.local/bin/greetings.sh: No such file or directory

At this point, the script can only be run from your home directory again using ./greetings.sh.

Conclusion

In this article, you learned how to write a bash script and execute it in multiple ways. You also saw how to make scripts available system-wide by placing them in PATH folders: /usr/local/bin/ for every user, or ~/.local/bin/ for only your personal account.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: