Running a .sh file means executing a shell script — a text file containing commands that Linux runs in order

A .sh file is a plain text file that holds a list of commands for your Linux system to run automatically. Instead of typing each command yourself, you save them in a file and tell Linux to run the whole list at once. The file extension .sh signals that it is a shell script, but Linux does not care about the extension — it cares whether the file has permission to run and whether it starts with the right instruction telling Linux which interpreter to use.

To run a .sh file, you need two things: the file must have execute permission, and you must tell your system where to find it. Most .sh files you read or create do not have execute permission by default, so the first step is always to add it. After that, you run the file by typing its path in the terminal.

Key Takeaways

  • A .sh file is a text file containing Linux commands that run in order when you execute the file.
  • Before you can run a .sh file, you must give it execute permission using the chmod command.
  • You run a .sh file by typing its path in the terminal, usually starting with ./ if the file is in your current folder.
  • The first line of a .sh file should be #!/bin/bash or #!/bin/sh to tell Linux which interpreter to use.
  • You can run a .sh file without execute permission by typing bash filename.sh or sh filename.sh instead.

Give the file execute permission with chmod

When you create a new .sh file or read one, it does not have execute permission by default. You add execute permission using the chmod command, which changes file permissions. Open your terminal, navigate to the folder where the .sh file is located, and type:

chmod +x filename.sh

This command adds execute permission for everyone who can access the file. The +x means "add execute permission". After you run this command, the file is ready to execute. You only need to do this once per file — the permission stays until you remove it.

Run the file from the terminal

Once the file has execute permission, you run it by typing its path in the terminal. If the file is in your current folder, type:

./filename.sh

The ./ means "in the current folder". Linux requires this because it does not search your current folder for commands by default — it searches specific system folders. If the file is in a different folder, type the full path instead, like /home/username/scripts/filename.sh or ~/scripts/filename.sh (the tilde ~ means your home folder).

When you press Enter, Linux reads the first line of the file to find out which interpreter to use, then runs each command in order. Any output the commands produce appears in your terminal.

The first line tells Linux which interpreter to use

The first line of a .sh file should be a shebang — a special instruction that tells Linux which program should run the file. The most common shebang is:

#!/bin/bash

This tells Linux to use the Bash shell to interpret the commands. Some systems use #!/bin/sh instead, which is a more basic shell that works on almost every Linux system. If you are writing your own .sh file, start with #!/bin/bash unless you know you need maximum compatibility with older systems.

The shebang must be the very first line, with no spaces before the #. If the file does not have a shebang, Linux will still try to run it, but it may use the wrong interpreter or fail silently.

Run a .sh file without changing permissions

If you do not want to change the file's permissions, you can run it by calling the shell interpreter directly. Type:

bash filename.sh

This tells your system to use Bash to run the file, regardless of whether it has execute permission. You can also use sh filename.sh to use the basic shell instead. This method is useful when you read a script and want to run it once without permanently changing its permissions, or when you are on a system where you cannot change permissions.

Understand what happens when a .sh file runs

When you execute a .sh file, Linux creates a new shell process and feeds each line of the file to that shell, one at a time. The shell reads the line, understands what command it is, and runs it. If a command fails, the shell usually stops and reports the error — though you can write the script to ignore errors and keep going if you want.

Any output the commands produce (like text printed to the screen) appears in your terminal. Any files the script creates or modifies are created or modified on your actual system, so be careful running scripts you do not understand. If a script asks for your password, it will prompt you in the terminal just like a normal command would.

Common reasons a .sh file will not run

If you try to run a .sh file and nothing happens, or you see an error, check these things first. The most common problem is missing execute permission — you will see a message like "Permission denied". Run chmod +x filename.sh and try again.

If you see "command not found", you may have typed the path wrong. Make sure you included ./ if the file is in your current folder, or the full path if it is elsewhere. If you see "No such file or directory", the file does not exist in the location you specified — check the spelling and the folder.

If the script runs but produces unexpected results, the problem is usually in the script itself, not in how you ran it. Check that the first line is a valid shebang, and that each command in the file is spelled correctly.

Frequently Asked Questions

What is the difference between .sh and other file types in Linux?

.sh is a shell script — a text file of commands. Other common types are .py for Python scripts, .rb for Ruby, and files with no extension that are still executable. The extension is just a label for humans; Linux cares about the shebang line and the execute permission.

Do I have to use chmod every time I want to run a .sh file?

No. Once you add execute permission with chmod +x, the file keeps that permission. You only need to run chmod once per file. If you delete the file and create a new one with the same name, you will need to chmod the new file.

Can I run a .sh file from a different folder without typing the full path?

If the file is in a folder that is in your system's PATH (a list of folders Linux searches for commands), you can run it by name alone. Most user files are not in PATH, so you usually need to type the path. Using ./ for files in your current folder is the standard way.

What does the shebang line actually do?

The shebang tells Linux which interpreter to use. #!/bin/bash means use Bash, #!/bin/sh means use the basic shell, and #!/usr/bin/python means use Python. If there is no shebang, Linux guesses based on the file extension or uses your default shell, which can cause problems.

Is it safe to run .sh files from the internet?

Only run .sh files from sources you trust. A .sh file can do anything on your system — delete files, install software, steal data. Always read the contents of a script before running it. Open it in a text editor and look at the commands to understand what it does.