How to Execute a Bash Script from Any Directory

Updated on 03 September, 2025
Learn how to create, run, and make bash scripts globally accessible in Linux using PATH directories.
How to Execute a Bash Script from Any Directory header image

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.

  1. Create and edit a greetings.sh file in your home directory.

    console
    $ nano greetings.sh
    
  2. 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.

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.

  1. 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.

  2. 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.
  3. 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.

  1. 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.

  2. Run the script with the sh command.

    console
    $ sh greetings.sh
    

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

  3. 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.

    Note
    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.
  4. 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
    Note
    Without making the script executable using the chmod +x command, this would not work.
  5. Run the bash script from any directory.

    1. Make a test directory test.

      console
      $ mkdir test
      
    2. 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.

Make the Script Executable for All Users

In the steps below, view your PATH environment variable and move your bash script to /usr/local/bin/.

  1. 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.

  2. Navigate back to your home directory.

    console
    $ cd
    
  3. Move your bash script to /usr/local/bin.

    console
    $ sudo mv greetings.sh /usr/local/bin/
    
  4. 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.

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/.

  1. Remove from /usr/local/bin/.

    console
    $ sudo rm /usr/local/bin/greetings.sh
    
  2. Remove the script from ~/.local/bin/.

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

    You own this directory; therefore, sudo is not required.

  3. 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.

Tags:

Comments

No comments yet.