What a .sh file is and why you might need to run one
A .sh file is a plain text file that contains a list of commands for your computer to run in order, one after another. The ".sh" stands for "shell script" — shell being the name of the program that reads and executes those commands. Think of it like a recipe: instead of typing each instruction into your terminal one at a time, you put them all in a file and tell the computer to work through the whole recipe at once.
You'll most often encounter .sh files when installing software, setting up development tools, or running maintenance tasks. Someone has already figured out the exact sequence of steps needed, written them down in a .sh file, and shared it with you so you don't have to remember or type each step yourself.
.sh files only work on Mac and Linux computers. Windows uses a different system (batch files, PowerShell scripts, or Windows Subsystem for Linux). If you're on Windows, you'll need to check whether the .sh file you have comes with Windows-specific instructions.
Key Takeaways
- A .sh file is a text file containing a series of commands that run one after another when you execute it.
- On Mac or Linux, you open the Terminal process, navigate to the folder containing the .sh file, and run it with a command like bash filename.sh or ./filename.sh.
- Before running a .sh file from the internet, open it in a text editor to see what commands it will actually run, so you know what you're agreeing to.
- If you get a "permission denied" error, the file needs permission to run — use chmod +x filename.sh to grant that permission first.
Opening Terminal and navigating to your file
To run a .sh file, you need to use the Terminal process, which lets you type commands directly to your computer. On Mac, open Spotlight (press Command + Space), type "Terminal", and press Enter. On Linux, look for Terminal in your applications menu, or press Ctrl + Alt + T on most distributions.
Once Terminal is open, you need to navigate to the folder where your .sh file is located. If the file is on your Desktop, type cd Desktop and press Enter. If it's in your Downloads folder, type cd Downloads. The cd command means "change directory" — it moves you into that folder. You can type ls and press Enter to see a list of files in the current folder, which confirms your .sh file is there.
If your file is nested deeper — for example, inside a folder called "project" on your Desktop — you would type cd Desktop/project. You can chain folder names together with forward slashes to move multiple levels at once.
Running the file with the correct command
Once you're in the same folder as your .sh file, you have two ways to run it. The first is bash filename.sh, replacing "filename" with the actual name of your file. For example, if your file is called "install.sh", you would type bash install.sh and press Enter. This command explicitly tells your computer to use the bash program to read and run the file.
The second way is ./filename.sh, which runs the file directly. The ./ means "in the current folder". This method only works if the file has permission to run — if you get an error saying "permission denied", skip ahead to the next section.
Either method will work in most cases. The bash filename.sh approach is more reliable if you're unsure whether the file has the right permissions set. Once you press Enter, the script will start running, and you'll see output in the Terminal as each command executes.
Fixing "permission denied" errors
If you try to run ./filename.sh and get an error that says "permission denied", it means the file doesn't have permission to run as a program yet. This is a safety feature — files don't automatically become executable just because they have a .sh extension.
To fix this, type chmod +x filename.sh and press Enter. The chmod command changes permissions, the +x means "add execute permission", and then you name the file. After you run this command, try ./filename.sh again, and it should work.
You only need to do this once per file. After you've granted permission, you can run that file as many times as you want without repeating the chmod step.
Reading the file before you run it
If the .sh file came from the internet or from someone you don't know well, open it in a text editor first to see what it actually does. Right-click the file, choose "Open With", and select a text editor like TextEdit (Mac) or gedit (Linux). You'll see the commands written out in plain language.
Look for anything that seems suspicious — commands that delete files, commands that read and run other files, or commands that ask for your password. If you don't understand what a command does, search for it online before running the script. This takes five minutes and protects you from accidentally running something harmful.
Legitimate scripts usually have comments at the top (lines starting with #) that explain what the script does. If there are no comments and the commands are intentionally obscured, that's a red flag.
Troubleshooting common problems
If the script starts running but stops with an error, read the error message carefully — it usually tells you what went wrong and on which line. Common problems include: the script is trying to use a program you don't have installed, the script is looking for a file in the wrong location, or the script needs you to be in a specific folder.
If the script needs administrator access (for example, to install software system-wide), you may need to run it with sudo bash filename.sh instead. The sudo command means "run this as an administrator" and will ask for your password. Only use sudo if the instructions specifically tell you to, or if you've read the script and understand why it needs that level of access.
If nothing happens when you run the script, check that you're in the correct folder by typing pwd (which shows your current location) and ls (which lists files). Make sure the filename you're typing matches exactly — capitalization matters, and spaces in filenames need special handling.
Understanding what happens while the script runs
While a .sh file is running, you'll see text appearing in the Terminal showing the progress of each command. Some scripts run silently with almost no output, while others print detailed information about what they're doing. This is normal — the script is just telling you what's happening.
If a script seems to be stuck or frozen, it might be waiting for input from you. Look carefully at the Terminal to see if there's a prompt asking you to type something or press Enter. If you're sure it's actually frozen (not just running slowly), you can press Ctrl + C to stop it.
Once the script finishes, you'll see a new command prompt appear, ready for you to type the next command. At that point, the script has completed, and any changes it was supposed to make have been done.
Frequently Asked Questions
Can I run a .sh file by double-clicking it?
On Mac, double-clicking a .sh file usually opens it in a text editor instead of running it. On Linux, it depends on your desktop environment — some will ask what you want to do, others will open it in an editor. The Terminal method is more reliable and shows you what the script is actually doing, so it's the recommended approach.
What's the difference between bash filename.sh and ./filename.sh?
Both run the same script, but bash filename.sh explicitly uses the bash program to read the file, while ./filename.sh runs the file directly using whatever program is specified inside it. The bash method works even if the file doesn't have execute permission, making it more forgiving for beginners.
Do I need to understand what's in the .sh file to run it?
You should at least skim it to make sure it's not doing something harmful, but you don't need to understand every command. If the script came from a trusted source (like official software documentation) and the commands look reasonable, it's safe to run. When in doubt, search online for the script name to see what other people say about it.
Can I edit a .sh file and save my changes?
Yes — open it in any text editor, make your changes, and save it. Be careful with spacing and punctuation, because shell scripts are strict about syntax. If you're not sure what you're doing, make a copy of the original file first so you have a backup.
Why do some scripts need sudo and others don't?
Scripts that install software system-wide, change system settings, or write to protected folders need administrator access, which is what sudo provides. Scripts that only affect your user account or create files in your home folder don't need it. The instructions for the script should tell you whether to use sudo.