What a .sh file is and why you need to execute it

A .sh file is a shell script — a text file containing a list of commands that Linux runs one after another, the same way you would type them into the terminal yourself. When you execute a .sh file, you are telling Linux to read that file and perform every instruction inside it. This saves you from typing the same sequence of commands over and over.

The .sh extension is just a label. Linux does not actually need it to run a script, but it tells you and other people that the file contains commands meant to be executed rather than read as plain text. You might encounter .sh files when you read software, install tools, or run maintenance tasks on your system.

Key Takeaways

  • A .sh file must have execute permission before Linux will run it; you add this permission with the chmod command.
  • The most common way to execute a .sh file is to type the full path to the file (or ./ if it is in your current folder) followed by the filename.
  • If a script fails to run, check that it starts with #!/bin/bash on the first line and that you have permission to read and execute it.
  • Running a script with bash scriptname.sh works even if the file does not have execute permission, which is useful for troubleshooting.

Adding execute permission to a .sh file

Before you can run a .sh file, Linux needs to know you are allowed to execute it. By default, files you read or create do not have execute permission. You add this permission using the chmod command in the terminal.

Open a terminal and navigate to the folder containing your .sh file. Type this command and press Enter:

chmod +x scriptname.sh

Replace scriptname.sh with the actual name of your file. The +x means "add execute permission for everyone." After you run this command, the file is ready to execute. You only need to do this once per file.

If you want to check whether a file already has execute permission, type ls -l scriptname.sh. Look at the first group of letters. If you see an x in there (like -rwxr-xr-x), the file can be executed. If you see a dash instead (like -rw-r--r--), it cannot.

Running the script from the terminal

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

./scriptname.sh

The ./ means "in the current folder." Press Enter and the script will run. If the file is in a different folder, type the full path instead:

/home/username/scripts/scriptname.sh

Replace the path with wherever your file actually lives. You can also drag the file from your file manager into the terminal window, and it will paste the full path automatically — then press Enter to run it.

Some scripts print output to the terminal as they run. Others run silently in the background. Either way, the terminal will return to a command prompt when the script finishes.

Running a script without execute permission

If you need to run a .sh file but do not have execute permission (or do not want to change permissions), you can tell bash to read and run the file directly:

bash scriptname.sh

This works even if the file has no execute permission at all. It is also useful for testing a script or troubleshooting problems, because you can see exactly which bash interpreter is running the commands. Some people prefer this method because it is explicit — you are clearly telling the system to use bash, rather than relying on the first line of the file to specify which interpreter to use.

Understanding the first line of a .sh file

If you open a .sh file in a text editor, the very first line usually looks like this:

#!/bin/bash

This line is called a shebang. It tells Linux which program should interpret the commands in the file. In this case, /bin/bash is the bash shell. When you execute the file directly (using ./scriptname.sh), Linux reads this line and knows to use bash to run the rest of the file.

If the shebang is missing or wrong, the script may fail or behave unexpectedly. If you create your own .sh file, always start with #!/bin/bash on the first line. If you read a script and it will not run, open it in a text editor and check that this line is there and spelled correctly.

Troubleshooting when a script will not run

If you try to execute a .sh file and get an error, check these things in order. First, make sure the file has execute permission by running ls -l scriptname.sh and looking for the x in the permissions. If it is missing, add it with chmod +x.

Second, open the file in a text editor and confirm the first line is #!/bin/bash with no extra spaces or characters before or after it. Third, try running the script with bash directly: bash scriptname.sh. If this works but ./scriptname.sh does not, the problem is usually the shebang line or the permissions.

If you get a "command not found" error, the script may be trying to run a program that is not installed on your system. If you get a "permission denied" error even after adding execute permission, you may not have permission to read the file itself — try chmod +r scriptname.sh to add read permission as well.

Running scripts from other folders

If you are in one folder and want to run a script in another folder without typing the full path every time, you can add the script folder to your system PATH. This is more advanced, but it means you can type just the script name from anywhere.

For most everyday use, it is simpler to either navigate to the folder where the script lives (using cd in the terminal) or type the full path. If you find yourself running the same script often, you can also create a shortcut or alias in your terminal configuration file, but that is beyond what most people need.

Frequently Asked Questions

What is the difference between ./ and typing the full path?

./ means "in the current folder" — it is a shortcut so you do not have to type the entire path. If your script is in /home/username/scripts and you are already in that folder, typing ./scriptname.sh is faster than typing /home/username/scripts/scriptname.sh. Both do the same thing.

Can I run a .sh file by double-clicking it in the file manager?

It depends on your Linux desktop environment. Some will ask whether you want to run it or edit it. Others will open it in a text editor by default. The terminal method is more reliable and works the same way every time, so it is the best approach if you run scripts regularly.

Do I need to be an administrator to run a .sh file?

Not always. You can run any script you have permission to execute. Some scripts need administrator (root) access to do their job — for example, installing software system-wide. If a script needs root access, you will get a permission error. You can then run it with sudo bash scriptname.sh, which will ask for your password.

What if the script runs but does nothing?

The script may have completed successfully but produced no visible output. Try running it with bash directly and look for any error messages: bash scriptname.sh. You can also open the file in a text editor to see what commands it is supposed to run, then check whether those programs are installed on your system.

Can I edit a .sh file after I have already run it?

Yes. Editing a script does not affect previous runs. Open it in any text editor, make your changes, save the file, and run it again. You do not need to change permissions again unless you remove the execute permission by accident.