Running a .sh file means telling Linux to execute the commands inside it
A .sh file is a text file containing a list of Linux commands that run one after another. To run it, you need to give Linux permission to execute it and then tell it to start. The most common way is to open a terminal, navigate to the folder where the file sits, and use one of two commands: bash filename.sh or ./filename.sh after you have marked the file as executable. Which one you use depends on whether you want to change the file's permissions first.
Both methods work on any Linux system. The bash method is faster if you only need to run the file once. The chmod and ./ method is faster if you plan to run it many times. Neither method is wrong — they are just different paths to the same result.
Key Takeaways
- The bash filename.sh command runs a .sh file without changing its permissions, and works even if the file is not marked executable.
- The ./filename.sh command requires you to first mark the file executable using chmod +x filename.sh, but is faster if you plan to run it many times.
- You must be in the same folder as the .sh file, or type the full path to it, for either command to work.
- If you get a "permission denied" error with ./filename.sh, the file is not marked executable yet — use chmod +x first.
- The first line of a .sh file should be #!/bin/bash so Linux knows which program should read the commands inside.
The quickest way: bash filename.sh
If you want to run a .sh file right now without changing anything, use the bash command. Open a terminal, move to the folder where your .sh file is, and type bash filename.sh (replace "filename" with the actual name). This tells the bash program to read and run the commands in that file.
This method works whether or not the file is marked as executable, so it is the fastest route when you just need to run something once. You do not have to remember to change permissions first. The downside is that you type more each time you run it, so if you plan to execute the same file many times, the other method becomes faster.
The standard way: chmod then ./filename.sh
If you plan to run a .sh file repeatedly, mark it executable first using chmod +x filename.sh. This changes the file's permissions so Linux knows it is allowed to run. After that, you can type ./filename.sh to start it — the dot and slash tell Linux to look in the current folder.
Once a file is marked executable, you only have to type ./filename.sh each time, which is shorter than bash filename.sh. The chmod step is a one-time action per file. If you forget to run chmod first and try ./filename.sh, you will see "permission denied" — that is the signal to go back and run the chmod command.
Finding and opening the right folder in terminal
Both methods require you to be in the same folder as your .sh file. Open a terminal and use cd to move to that folder. For example, if your file is on your desktop, type cd Desktop. If it is in your Documents folder, type cd Documents. You can type ls to list the files in the current folder and confirm your .sh file is there.
If your .sh file is buried several folders deep, you can type the full path instead of navigating step by step. For example, bash /home/username/Documents/scripts/myscript.sh runs the file no matter which folder you are currently in. Replace "username" with your actual username and adjust the path to match where your file actually sits.
What to do when you see an error
If you type ./filename.sh and see "permission denied", the file is not marked executable. Run chmod +x filename.sh and try again. If you see "command not found" or "no such file or directory", you are either in the wrong folder or typed the filename wrong — use ls to check what files are actually there and what their exact names are.
If the .sh file runs but produces errors about missing commands, the script itself may need something installed on your system first. Read the error message carefully — it usually tells you what program or file is missing. If the script runs but does nothing, check whether the first line is #!/bin/bash. Without that line, Linux may not know which program should read the file.
Understanding what the .sh file does before running it
Before you run a .sh file from someone else or from the internet, open it in a text editor to see what commands it contains. Right-click the file, choose "Open With", and select a text editor like gedit or nano. Read through the commands to understand what the script will do — will it delete files, change settings, install software, or something else?
This step protects you from running scripts that might harm your system or steal information. If you do not recognize most of the commands, ask someone you trust to review it first. Once you understand what it does and you trust the source, you can safely run it using either method described above.
Running .sh files from other locations
If your .sh file is not in the current folder, you have three options. First, navigate to its folder using cd and run it from there. Second, type the full path: bash /home/username/path/to/filename.sh or ./path/to/filename.sh (after marking it executable). Third, move or copy the file to your current folder using cp or mv, then run it.
The full path method is useful when you have a script in a fixed location that you want to run from anywhere. For example, if you have a backup script at /home/username/scripts/backup.sh, you can type bash /home/username/scripts/backup.sh from any folder and it will run. This is why many system administrators keep useful scripts in a dedicated folder and use the full path to run them.
Frequently Asked Questions
Do I have to use bash, or can I use sh instead?
You can use sh filename.sh instead of bash filename.sh. The difference is that bash is more feature-rich, but sh is more portable across different Unix systems. For most scripts written for Linux, bash works fine. If the script's first line says #!/bin/sh, use the sh command; if it says #!/bin/bash, use bash.
What does chmod +x actually do?
It adds execute permission to the file so Linux knows it is allowed to run. Files have three types of permission: read, write, and execute. The +x means "add execute permission". You only have to do this once per file. If you want to remove execute permission later, use chmod -x filename.sh.
Can I run a .sh file by double-clicking it in the file manager?
On some Linux systems, yes — if the file is marked executable and the file manager is set to run scripts. On others, double-clicking opens it in a text editor instead. The terminal method is more reliable because it always works the same way. If you want double-click to work, mark the file executable with chmod +x first, then right-click it, choose Properties, and look for an option to run it as a program.
What if the .sh file needs to run as root or with sudo?
Type sudo bash filename.sh or sudo ./filename.sh (after marking it executable). The sudo command runs the script with administrator privileges. You will be asked for your password. Only use sudo with scripts you trust completely, because they will have access to sensitive parts of your system.
How do I stop a .sh file that is running?
Press Ctrl+C in the terminal. This sends an interrupt signal that stops most running scripts. If that does not work, the script may be stuck — wait a moment and try Ctrl+C again. If the script is running in the background, you can use kill or killall commands, but Ctrl+C is the normal way to stop a script you started in the terminal.