The three ways to run a .sh file

A .sh file is a shell script — a text file containing Linux commands that run one after another. To run it, you have three main options: use bash or sh to execute the file directly, make the file executable and run it as a program, or source it into your current shell session. Which one you choose depends on whether the script needs to modify your current environment or run independently.

The most common approach is to make the file executable with the chmod command, then run it by typing its path. This is what most scripts expect. But if a script isn't executable or you want to run it without changing permissions, you can pass it to bash or sh as an argument instead.

Key Takeaways

  • Run a script with bash script.sh or sh script.sh without needing to change file permissions.
  • Make a script executable once with chmod +x script.sh, then run it by typing ./script.sh from the same directory.
  • Use source script.sh or . script.sh only if the script needs to change variables or settings in your current shell session.
  • If you get a "permission denied" error, the file is not executable — use chmod +x or run it through bash instead.
  • The first line of a script should be #!/bin/bash so the system knows which shell to use when you run it directly.

Running a script without making it executable

If you want to run a .sh file without changing its permissions, pass it as an argument to the bash command. Open your terminal, navigate to the directory where the script is stored, and type:

bash script.sh

Replace script.sh with the actual filename. This tells bash to read and execute the commands inside the file. You can also use sh instead of bash — sh is more portable across different Unix systems, but bash is more common on Linux. The file does not need to be executable for this method to work.

If the script is in a different directory, include the path. For a script in your home directory, use bash ~/script.sh. For a script in a subdirectory called scripts, use bash ./scripts/script.sh.

Making a script executable and running it directly

To run a script the way most Linux programs work, make it executable first. In your terminal, navigate to the script's directory and type:

chmod +x script.sh

This adds execute permission to the file. You only need to do this once. After that, you can run the script by typing:

./script.sh

The ./ tells the system to look for the script in the current directory. Without it, the system searches your PATH (a list of standard directories) and won't find a script in your working folder. If the script is in a different directory, use the full path: ~/scripts/script.sh or /home/username/scripts/script.sh.

For this method to work properly, the script should start with a shebang line — the first line should be #!/bin/bash or #!/bin/sh. This tells the system which shell to use when running the script. If the shebang is missing, the system uses the default shell, which may not work as intended.

Using source to run a script in your current shell

Normally, when you run a script, it starts in a new shell session. Any variables or settings the script changes only affect that new session and disappear when the script ends. If you want the script to modify your current shell environment — for example, to change directories or set environment variables that persist — use the source command:

source script.sh

You can also use a dot (.) instead of source:

. script.sh

Both do the same thing. This method is useful for scripts that set up your environment, like loading aliases or changing your working directory. Most scripts you download or write do not need this — they run fine in their own session. Use source only when the script is designed to modify your current shell.

Passing arguments to a script

Scripts often need input from you. You can pass arguments when you run the script by typing them after the filename, separated by spaces:

bash script.sh argument1 argument2

Or if the script is executable:

./script.sh argument1 argument2

Inside the script, these arguments are available as $1, $2, and so on. $1 is the first argument, $2 is the second, and $0 is the script's own name. If an argument contains spaces, wrap it in quotes: ./script.sh "hello world".

Troubleshooting common errors

If you see "permission denied" when trying to run a script with ./script.sh, the file is not executable. Use chmod +x script.sh to fix it, or run the script through bash instead: bash script.sh.

If you see "command not found" or the script does not run, check that the path is correct. Make sure you are in the right directory or use the full path to the script. Also check that the script exists and is spelled correctly.

If the script runs but produces unexpected output or errors, the problem is usually inside the script itself, not with how you ran it. Open the file in a text editor and check the commands. You can also run the script with debugging enabled: bash -x script.sh shows each command as it runs, which helps you see where things go wrong.

Frequently Asked Questions

What is the difference between bash and sh?

bash is the Bourne Again Shell, the default on most Linux systems. sh is the original Bourne shell, more portable but with fewer features. For scripts you write yourself, bash is fine. For scripts that need to run on many different Unix systems, use sh and avoid bash-specific features.

Do I have to use chmod every time I run a script?

No. chmod +x changes the file's permissions permanently. Once you run it, the file stays executable. You only need to run chmod once per script. If you prefer not to use chmod, you can always run scripts with bash script.sh instead.

What does the shebang line do?

The shebang (#!/bin/bash) tells the system which interpreter to use when you run the script directly. Without it, the system uses your default shell, which may not understand all the commands in the script. Always include a shebang as the first line of any script you plan to run with ./script.sh.

Can I run a script from a different directory?

Yes. Use the full path to the script: bash /home/username/scripts/script.sh or ~/scripts/script.sh if the script is executable. The ~ represents your home directory. You do not need to be in the script's directory to run it.

What if the script needs to change my working directory?

If you run the script normally, it changes directory only within that script session — you return to your original directory when it ends. To make the directory change stick in your current shell, use source script.sh instead of running it directly.