The basic way to run a .sh file

To run a .sh file in Linux, you need to make it executable and then call it from the terminal. The most direct method is to navigate to the directory containing the file, make it executable with the chmod command, and then run it with a dot-slash prefix.

Open your terminal and type this command in the directory where your .sh file lives:

chmod +x filename.sh

Then run the file with:

./filename.sh

The chmod +x part tells Linux that this file is allowed to run as a program. The dot-slash (./) tells Linux to look for the file in the current directory. Without it, Linux searches your system PATH instead and usually won't find a file in your working folder.

Key Takeaways

  • You must make a .sh file executable with chmod +x filename.sh before you can run it.
  • Run the file from its directory using ./filename.sh, not just the filename alone.
  • If you get a "permission denied" error, the file is not marked executable — use chmod +x again.
  • You can run a script without making it executable by calling the shell directly: bash filename.sh or sh filename.sh.
  • The first line of a .sh file should be a shebang (#!/bin/bash) so the system knows which shell to use.

Understanding file permissions and the chmod command

Linux files have three permission types: read, write, and execute. A .sh file can exist on your system but still be blocked from running if it doesn't have execute permission. The chmod command changes these permissions.

When you type chmod +x filename.sh, the +x adds execute permission for everyone who can access the file. If you want to restrict it to only your user account, use chmod u+x filename.sh instead. The u means "user" (just you), while +x means "add execute".

You can check a file's current permissions by typing ls -l filename.sh. If the file is executable, you'll see an x in the permission string. If you see -rw-r--r--, the file is not executable. If you see -rwxr-xr-x, it is.

Running a script without changing permissions

If you don't want to modify the file's permissions, you can run a .sh file by calling the shell directly. Type either of these commands:

bash filename.sh

or

sh filename.sh

This approach bypasses the execute permission requirement because you're explicitly telling the system to use the bash or sh shell to read and run the file. It's useful when you're working with files you can't modify permissions on, or when you want to test a script without permanently changing it.

The difference between bash and sh matters only if your script uses bash-specific features. Most simple scripts work with either one. If your script uses advanced bash syntax and you call it with sh, it may fail or behave unexpectedly.

The shebang line and how it works

The first line of a well-written .sh file is called a shebang. It looks like this:

#!/bin/bash

This line tells Linux which shell interpreter to use when running the script. The #! is the shebang marker, and /bin/bash is the path to the bash shell. When you run the file with ./filename.sh, Linux reads this line and knows to use bash.

If your script uses sh instead, the shebang would be #!/bin/sh. Some systems have bash at a different path, so you can check where it lives by typing which bash in your terminal. The shebang must be the very first line of the file, with no spaces or blank lines before it.

Without a shebang, Linux may try to guess which shell to use, or it may fail entirely. Adding one makes your script portable and clear about its requirements.

Troubleshooting common errors

If you see "permission denied" when you try to run a .sh file, the file is not marked executable. Run chmod +x filename.sh and try again. If you see "command not found", you may have typed the filename wrong, or you may have forgotten the dot-slash prefix. Always use ./filename.sh when running a file in your current directory.

If you see "No such file or directory", check that the filename is spelled correctly and that you're in the right directory. Type pwd to see your current location and ls to list files in that folder.

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

Running scripts from other directories

If your .sh file is not in your current directory, you have two options. You can type the full path to the file:

./path/to/filename.sh

or

/home/username/path/to/filename.sh

Alternatively, you can add the directory containing your scripts to your system PATH, so you can call them by name from anywhere. This is more advanced and usually only worth doing if you have scripts you use regularly from many different directories.

For one-off scripts or scripts you keep in a specific folder, using the path is simpler and clearer. It's always obvious where the script is coming from, which prevents confusion if another script with the same name exists elsewhere on your system.

Running scripts with arguments and input

Many .sh files accept arguments — extra information you pass to the script when you run it. For example:

./filename.sh argument1 argument2

Inside the script, these arguments are available as $1, $2, and so on. The script itself determines what arguments it expects and what it does with them. Check the script's documentation or open it in a text editor to see what arguments it needs.

Some scripts also prompt you for input while they're running. If a script asks you a question, type your answer and press Enter. The script will continue with whatever you typed.

Frequently Asked Questions

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

No. Once you run chmod +x filename.sh, the file stays executable. You only need to do it once per file. If you later modify the file, it remains executable.

What's the difference between running a script with ./ and running it with bash?

Using ./filename.sh requires the file to be executable and uses the shebang line to decide which shell to use. Using bash filename.sh explicitly tells the system to use bash, regardless of permissions or shebang. Both work, but ./ is the standard approach for executable scripts.

Can I run a .sh file on Windows?

Not directly. Windows doesn't natively understand shell scripts. However, if you have Windows Subsystem for Linux (WSL) installed, you can run .sh files inside the Linux environment. On older Windows systems without WSL, you would need a tool like Git Bash or Cygwin.

Why do I need the dot-slash (./) prefix?

Linux searches your PATH (a list of directories) when you type a command. Your current directory is usually not in the PATH for security reasons. The dot-slash tells Linux to look in the current directory instead. Without it, Linux won't find a script in your working folder.

What if a script needs to run as root or with sudo?

You can run any script with elevated permissions by typing sudo ./filename.sh. This prompts you for your password and then runs the script with administrator privileges. Use this only for scripts you trust, since they'll have access to sensitive parts of your system.