What a shell script is and why you might run one
A shell script is a text file containing a list of commands that your computer runs one after another. Instead of typing each command yourself, you save them in a file with a .sh extension and run the whole file at once. This is useful when you need to repeat the same set of steps regularly, or when someone has written a script that does something you need — like backing up files, organizing folders, or installing software.
The steps to run a shell script differ depending on whether you use Windows, Mac, or Linux. All three can run .sh files, but the method varies because each operating system handles permissions and command execution differently.
Key Takeaways
- On Mac and Linux, you must give a shell script permission to run before you can execute it, using the chmod command in Terminal.
- On Windows, you need either Windows Subsystem for Linux (WSL) or a third-party tool like Git Bash to run .sh files natively.
- The simplest way to run a script is to open Terminal or Command Prompt, navigate to the folder where the script is saved, and type the command to execute it.
- If a script does not run, check that the file extension is actually .sh and that you have saved any changes you made to the file.
Running a shell script on Mac
On Mac, open the Terminal process (found in Applications > Utilities). Navigate to the folder where your .sh file is saved by typing cd followed by the path to that folder. For example, if your script is on your Desktop, type cd ~/Desktop and press Enter.
Before you can run the script, you must give it permission to execute. Type chmod +x filename.sh (replacing "filename" with the actual name of your script) and press Enter. This command tells your Mac that the file is allowed to run. You only need to do this once per file.
Now run the script by typing ./filename.sh and pressing Enter. The dot and slash tell your Mac to look for the file in the current folder. The script will begin executing, and you will see any output it produces in the Terminal window.
Running a shell script on Linux
The process on Linux is nearly identical to Mac. Open Terminal, navigate to the folder containing your .sh file using cd, then give the file permission to run with chmod +x filename.sh.
Execute the script by typing ./filename.sh and pressing Enter. If you want to run the script from any folder on your computer without typing the full path, you can move it to a folder that is already in your system's PATH, but for most everyday use, running it from its current location is simpler.
Running a shell script on Windows
Windows does not natively run .sh files the way Mac and Linux do. You have two main options: install Windows Subsystem for Linux (WSL), or use a tool like Git Bash.
Using Windows Subsystem for Linux (WSL) is the most direct route if you plan to run scripts regularly. WSL lets you run a Linux environment inside Windows. To set it up, open PowerShell as Administrator, type wsl --install, and follow the prompts. Once installed, open the WSL terminal, navigate to your script using cd, run chmod +x filename.sh, then execute it with ./filename.sh.
Using Git Bash is faster if you only need to run a script once or twice. read and install Git for Windows from git-scm.com. During installation, choose the option to add Git Bash to your context menu. Then right-click the folder containing your .sh file, select "Git Bash Here", and run the same commands as you would on Mac or Linux: chmod +x filename.sh followed by ./filename.sh.
Troubleshooting when a script will not run
If you type the command to run your script and nothing happens, or you see an error message, check these common problems first. Verify that the file extension is actually .sh and not .sh.txt — Windows sometimes hides the true extension. Open the file in a text editor to confirm it contains commands, not just plain text.
If you see a "permission denied" error on Mac or Linux, you forgot the chmod +x step. Run that command and try again. If you see "command not found", you may have typed the filename incorrectly, or you are not in the correct folder — use ls to list the files in your current folder and verify the exact name.
If you edited the script in a text editor on Windows and then moved it to Mac or Linux, the line endings might be in Windows format rather than Unix format. Most modern text editors can convert this automatically — open the file, look for a line ending option (often in the bottom right corner), and change it from CRLF to LF.
Understanding what happens when you run a script
When you execute a shell script, your computer reads the file line by line and runs each command in order. If one command fails, the script usually stops and shows you an error message. If all commands succeed, the script finishes and returns you to the command prompt.
Some scripts produce output you see in the Terminal window. Others run silently in the background. Some scripts ask for your password or require you to confirm an action by typing "yes" or "no". Read any documentation that came with the script to understand what it will do and what input it might need.
Frequently Asked Questions
Can I run a shell script by double-clicking it?
On Mac, if you have given the script permission to run with chmod, double-clicking it may open it in a text editor instead of executing it. Right-click the file, select "Open With", then choose "Terminal" to run it. On Windows, double-clicking a .sh file will not work without WSL or Git Bash installed. On Linux, double-clicking usually opens the file for editing rather than running it.
What if the script needs to run with administrator or sudo permissions?
If a script modifies system files or installs software, it may need elevated permissions. On Mac and Linux, type sudo ./filename.sh instead of ./filename.sh. You will be asked to enter your password. On Windows with WSL, use the same sudo command. Use sudo only for scripts you trust, since it gives the script full control of your computer.
How do I know what a shell script does before I run it?
Open the .sh file in a text editor like Notepad, TextEdit, or VS Code. You will see a list of commands. If you recognize the commands, you can understand what the script does. If the script is complex or contains unfamiliar commands, search online for the command names, or ask the person who gave you the script what it does before running it.
Can I edit a shell script after I have already run it?
Yes. Open the file in a text editor, make your changes, and save. You do not need to run chmod again — the permission you set earlier stays with the file. The next time you run the script, it will use the updated commands.