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, like a recipe. Instead of typing each command yourself, you save them in a file and tell your computer to execute the whole file at once. Shell scripts end in .sh and are most common on Mac and Linux, though Windows can run them too with the right setup.
You might encounter a shell script when installing software, setting up development tools, or automating repetitive tasks. The script does the work for you — it might download files, move folders around, change settings, or launch programs. Before you run any script, you should know what it does, because scripts can make changes to your system that are hard to undo.
Key Takeaways
- On Mac and Linux, you open Terminal, navigate to the folder holding the script, and type bash filename.sh or ./filename.sh to run it.
- On Windows 10 and later, you can use Windows Subsystem for Linux (WSL) or PowerShell to run shell scripts, though native Windows batch files are more common.
- Before running any script, read it in a text editor to understand what commands it contains, especially if it came from the internet.
- Scripts sometimes need permission to run — on Mac and Linux, you may need to type chmod +x filename.sh first to unlock that permission.
- If a script fails, the error message usually tells you what went wrong — missing files, wrong folder location, or a command your system doesn't recognize.
Running a shell script on Mac or Linux
On Mac and Linux, the process is straightforward. Open the Terminal application (on Mac, search for it in Spotlight; on Linux, right-click the desktop or use your application menu). Type cd followed by the path to the folder where your script lives. For example, if the script is on your Desktop, type cd ~/Desktop and press Enter.
Once you are in the correct folder, type bash filename.sh (replace "filename" with the actual name) and press Enter. The script will run and you will see its output in the Terminal window. If the script needs to make changes to your system, it may ask for your password — this is normal and expected.
If you get an error saying "Permission denied", the script does not have permission to run yet. Type chmod +x filename.sh and press Enter, then try running the script again. This command unlocks the execute permission, which is required for shell scripts to run on these systems.
Running a shell script on Windows
Windows does not natively run shell scripts the way Mac and Linux do. Your options depend on which version of Windows you have. If you are on Windows 10 or later, the easiest path is Windows Subsystem for Linux (WSL), which lets you run Linux commands directly on Windows. To set it up, open PowerShell as Administrator, type wsl --install, and follow the prompts. After installation, you can open WSL and run shell scripts the same way you would on Linux.
If you do not want to install WSL, you can use PowerShell instead. Open PowerShell, navigate to your script folder using cd, then type powershell -ExecutionPolicy Bypass -File filename.sh. This tells PowerShell to run the script without blocking it. However, PowerShell is not a true shell environment, so some scripts may not work correctly.
For older Windows versions or if neither option works, you can use Git Bash, which is a free tool that gives Windows a Linux-like command environment. Download it from git-scm.com, install it, and then open Git Bash to run your scripts the same way you would on Mac or Linux.
Reading a script before you run it
Before running any script, especially one you downloaded from the internet, open it in a text editor to see what it actually does. Right-click the .sh file, choose "Open With", and select a text editor like Notepad (Windows), TextEdit (Mac), or gedit (Linux). You will see a list of commands — look for anything suspicious like commands that delete files, change passwords, or download unknown software.
If you do not understand what a command does, search for it online. A legitimate script from a trusted source should have comments (lines starting with #) that explain what each section does. If a script is intentionally hidden or obfuscated, that is a red flag — do not run it.
Pay special attention to commands that use sudo (on Mac and Linux) or run as Administrator (on Windows). These commands can make system-wide changes. If you are not sure why a script needs that level of access, ask the person or organization that gave you the script.
Fixing common errors when running scripts
The most common error is "command not found", which means your computer does not recognize one of the commands in the script. This usually happens because the script expects software that is not installed on your system. The error message will tell you which command failed — search for that command online to find out what software you need to install.
Another frequent problem is "No such file or directory", which means the script is looking for a file or folder that does not exist. Check that you are in the correct folder (type pwd to see where you are) and that the file names in the script match the actual file names on your system. File names are case-sensitive on Mac and Linux, so myfile.txt and MyFile.txt are different.
If the script runs but does nothing, it may have completed successfully without producing visible output. Some scripts only show a message if something goes wrong. If you are unsure, check whether the script was supposed to create files or make changes — look in the relevant folders to confirm the work was done.
Understanding script output and logs
When a script runs, it prints information to the Terminal or PowerShell window. This output tells you what the script is doing and whether it succeeded. Read this output carefully — it often contains clues about what went wrong if the script fails partway through.
For longer scripts that take time to run, you can save the output to a file so you can read it later. On Mac and Linux, type bash filename.sh > output.txt 2>&1 to save all output to a file called output.txt. On Windows PowerShell, type powershell -ExecutionPolicy Bypass -File filename.sh | Tee-Object -FilePath output.txt. This is useful if the script produces so much output that it scrolls off the screen.
When a script needs to run automatically
If you want a script to run on a schedule — for example, every morning at 8 AM or every Friday — you can set that up using your system's task scheduler. On Mac and Linux, this is called cron. On Windows, it is called Task Scheduler. Both tools let you specify a script and a time, and your computer will run it automatically without you having to do anything.
Setting up scheduled tasks is more advanced and depends on your specific script and system. If you need this, search for "cron on Mac" or "Task Scheduler on Windows" along with your script name to find instructions tailored to your situation.
Frequently Asked Questions
What is the difference between bash filename.sh and ./filename.sh?
Both run the script, but they work slightly differently. bash filename.sh explicitly tells the bash program to read and run the file. ./filename.sh runs the file directly using the interpreter specified in the first line of the script (usually bash). The second method requires the execute permission to be set first. Either works, but bash filename.sh is more reliable if you are unsure about permissions.
Can I run a shell script by double-clicking it?
On Mac, yes — if the script has execute permission, you can double-click it and it will run. On Linux, double-clicking usually opens the file in a text editor instead. On Windows, double-clicking will not work unless you have set up WSL or Git Bash. Using the Terminal or PowerShell is the most reliable method across all systems.
What does chmod +x do?
It adds execute permission to the file, which tells your system that the file is allowed to run as a program. Without this permission, the system treats it as a regular text file. You only need to do this once per script — after you set the permission, you can run the script as many times as you want.
Is it safe to run scripts from the internet?
Only if you trust the source. Read the script in a text editor first to see what it does. If it comes from a well-known project or organization with a good reputation, it is usually safe. If you cannot understand what the script does or it came from an unknown source, do not run it. When in doubt, ask someone with technical knowledge to review it.
Why does my script need my password?
Scripts that make system-wide changes (like installing software or changing settings) need administrator permission. Your system asks for your password to confirm that you really want to allow those changes. This is a security feature. If a script asks for your password and you do not understand why, do not enter it — read the script first to see what it is trying to do.