Running a .sh file means executing the commands inside it as a single action
A .sh file is a text file containing a list of Linux commands that run one after another. To run it, you give the file permission to execute, then call it by name. The simplest way is to type ./filename.sh in the terminal, assuming you are in the same folder where the file sits. Linux will read the file from top to bottom and perform each command in order.
The file needs two things before it will run: execute permission (a setting that marks it as runnable) and a shebang line at the very top (usually #!/bin/bash), which tells Linux which interpreter to use. Without execute permission, Linux will refuse to run it even if you try. Without the shebang, Linux may guess wrong about how to read the commands inside.
Key Takeaways
- Add execute permission to a .sh file by typing chmod +x filename.sh in the terminal.
- Run the file from its own folder by typing ./filename.sh, or from anywhere by typing the full path to the file.
- The first line of the file should be #!/bin/bash so Linux knows which command interpreter to use.
- You can also run a .sh file without execute permission by typing bash filename.sh instead.
Add execute permission before running the file
By default, a new .sh file does not have permission to run. You must grant that permission using the chmod command. Open a terminal, navigate to the folder containing your .sh file, and type:
chmod +x filename.sh
Replace filename.sh with the actual name of your file. The +x part means "add execute permission". After you run this command, the file is marked as runnable. You only need to do this once per file.
If you want to check whether a file already has execute permission, type ls -l filename.sh. Look at the first group of letters in the output. If you see an x in that group, the file can already run. If there is no x, you need to add it with chmod +x.
Run the file from the terminal using ./
Once the file has execute permission, run it by typing ./filename.sh in the terminal. The ./ part means "look in the current folder". Linux will then read the file and execute each command inside it in order.
For example, if your file is called backup.sh and it sits in your home folder, open a terminal, make sure you are in your home folder (type cd ~ to go there), then type:
./backup.sh
The terminal will run the script and show any output the commands produce. If something goes wrong, error messages will appear on screen so you can see what failed.
Run the file from any folder using the full path
If you are in a different folder and want to run a .sh file that is somewhere else, type the full path to the file instead of just the name. For example, if your script is at /home/username/scripts/backup.sh, you can type:
./home/username/scripts/backup.sh
Or you can use the tilde shortcut if the file is inside your home folder. The tilde (~) stands for your home folder, so ~/scripts/backup.sh means the same thing as /home/username/scripts/backup.sh.
Using the full path is useful when you have scripts stored in a central location and want to run them from anywhere without changing folders first.
Run a .sh file without execute permission using bash
If you do not want to change the file's permissions, you can run it by calling the bash interpreter directly. Type:
bash filename.sh
This tells bash to read and execute the file, even if it does not have execute permission. The file still needs the shebang line at the top, but the permission setting does not matter. This method is useful if you are working with a file you cannot modify permissions for, or if you want to run a script without permanently marking it as executable.
Make sure the file starts with the correct shebang line
The first line of a .sh file should be #!/bin/bash. This line is called the shebang, and it tells Linux which interpreter should read the file. Without it, Linux may try to use the wrong interpreter or fail to run the file at all.
Open your .sh file in a text editor and check the very first line. It should look exactly like this:
#!/bin/bash
If the file is missing this line, add it at the top before any other commands. Save the file and try running it again. The shebang must be on the first line with no spaces or characters before it.
Understand what happens when a script runs
When you run a .sh file, Linux creates a new bash session just for that script. It reads each line from top to bottom and executes the commands in order. If one command fails, the script usually stops and shows an error message. If all commands succeed, the script finishes and returns to the terminal prompt.
Any output the commands produce (like text printed to the screen) will appear in your terminal. If the script creates files or changes settings on your system, those changes happen just as if you had typed the commands yourself. This is why you should only run .sh files from sources you trust — they have the same power as you do to change your system.
If you want to see what a script does before running it, open the file in a text editor and read through the commands. This helps you understand what will happen and catch any obvious mistakes.
Frequently Asked Questions
What does "permission denied" mean when I try to run a .sh file?
It means the file does not have execute permission. Type chmod +x filename.sh to add it, then try running the file again. After that, ./filename.sh should work.
Can I run a .sh file from Windows or Mac?
Mac includes bash and can run .sh files the same way Linux does. Windows does not have bash built in, but you can use Windows Subsystem for Linux (WSL) to run Linux commands and .sh files. Older versions of Windows require additional software like Git Bash or Cygwin.
What if the script needs to run as root or with special permissions?
You can run a script with elevated permissions by typing sudo ./filename.sh. This asks for your password and then runs the script with administrator privileges. Only do this for scripts you wrote or trust completely, since they will have full control over your system.
How do I stop a script that is running?
Press Ctrl+C in the terminal. This sends an interrupt signal that stops most running scripts. If a script is stuck and will not respond to Ctrl+C, you can open another terminal and use the kill command, but this is rarely necessary.
Can I edit a .sh file after it is already running?
No. Once a script starts running, Linux reads the entire file into memory. Changes you make to the file while it runs will not take effect until you run the script again. Always save your changes and start a fresh run if you want to test new commands.