How to Run a Shell Script (.sh) on macOS

To run a shell script on macOS, open Terminal, go to the folder containing the .sh file, and run it with its interpreter. For example, bash hello.sh runs a Bash script without changing its permissions. If you want to launch it as a command, make it executable with chmod +x hello.sh, then run ./hello.sh. These methods work on current macOS releases.

Check the script before running it

A shell script is a plain-text file containing commands. Those commands run with the permissions of your user account, so a script can edit or delete files, install software, or send data over a network. Read a script you received before running it, especially if it asks for an administrator password or uses sudo. Only run scripts from sources you trust; changing a file’s permissions does not make its contents safe.

Apple describes shell scripts as text files that contain UNIX commands and explains how a shebang selects the shell that runs the script. See Apple’s Terminal guide to shell scripts.

Run a .sh script with Bash

This is the simplest way to run a Bash script. You do not need to add execute permission first.

  1. Create or locate the script. A shell script can be written in any plain-text editor. For a quick test, open TextEdit, choose Format > Make Plain Text, and enter these two lines:
#!/bin/bash
echo "Hello from macOS"

Save the file as hello.sh. In the save dialog, check that the name ends in .sh and that TextEdit has not added .txt. The first line is the shebang: it identifies Bash as the interpreter when the script is launched directly. The second line prints a short message.

TextEdit in plain-text mode showing hello.sh with a Bash shebang and an echo command
The sample hello.sh file contains a shebang followed by a command that prints text.
  1. Open Terminal and go to the file’s folder. Press Command-Space, type Terminal, and press Return. If you saved the file in Downloads, enter:
cd ~/Downloads

Press Return after each command. You can type ls to list files and confirm that hello.sh is there. Change Downloads to the folder you actually used. If a folder or file name contains spaces, put the path in quotation marks, such as cd "$HOME/My Scripts".

Terminal in the Downloads folder listing hello.sh after changing directories
Use cd to open the folder that contains the script, then check its name with ls.
  1. Run the script with Bash. Enter the script name after bash:
bash hello.sh

Terminal should display Hello from macOS. If the script uses Zsh syntax instead of Bash, run zsh hello.sh. The shell that opens interactively in Terminal is usually Zsh, but that does not mean every script should be run with Zsh; use the interpreter the script was written for. A script’s shebang often indicates that interpreter.

Run the script as an executable

You can also launch a script directly by its path. This is useful when you want to run it like a small command. Apple’s Terminal instructions for making a file executable use chmod for this step.

  1. Grant execute permission. In the same Terminal window and folder, run:
chmod +x hello.sh

This adds execute permission to this file. It is a one-time change unless permissions are later reset. Avoid using broader permission settings than needed; chmod +x is sufficient for a personal script you own.

Terminal showing chmod plus x applied to hello.sh
The chmod +x command marks hello.sh as executable.
  1. Launch it from the current folder. Type:
./hello.sh

The ./ tells the shell to run the file in the current directory. Without it, the shell generally searches folders listed in PATH rather than the current folder. If you are elsewhere, either use cd to return to the script’s folder or enter its full path, for example ~/Downloads/hello.sh.

Terminal running ./hello.sh and displaying Hello from macOS
Running ./hello.sh starts the executable script and shows its output.

Common errors and fixes

  • “Permission denied” after using ./hello.sh: confirm the filename, then run chmod +x hello.sh. Or use bash hello.sh if it is a Bash script and you do not need direct execution.
  • “No such file or directory”: confirm the folder with pwd, list its contents with ls, and check the spelling and capitalization. Make sure the file is not actually named hello.sh.txt.
  • “Command not found” inside the script: the script may rely on a utility that is missing or not on PATH. Check the script’s documentation before installing anything.
  • “Bad interpreter” or an error mentioning ^M: the first line may point to an unavailable interpreter, or the file may use Windows-style line endings. Check the shebang and save the file with Unix (LF) line endings.

Which method should you use?

Use bash hello.sh for a quick run when you know it is a Bash script. Use chmod +x hello.sh followed by ./hello.sh when you want to launch it directly and let its shebang choose the interpreter. Neither method installs missing dependencies, fixes incompatible commands, or verifies whether the script is trustworthy.

Leave a Comment

How to Screenshot on Mac: A Comprehensive Guide

How to Screenshot on Mac: A Comprehensive Guide

Learn how to screenshot on Mac with shortcuts for the full screen, an area, or a window, plus the Screenshot app, clipboard, editing, and save-location tips.

How to Add Multiple Images to a Single PDF on macOS

How to Add Multiple Images to a Single PDF on macOS

Combine multiple images into a single PDF on macOS using Finder or Preview. Control page order, avoid cropping, and fix missing pages without extra software.

How to View FPS on macOS: Steam and Apple’s Metal HUD

How to View FPS on macOS: Steam and Apple’s Metal HUD

Learn how to show FPS on a Mac with Steam’s performance overlay or Apple’s Metal Performance HUD, plus fixes when the counter does not appear.

How to Force Quit on a Mac: 5 Quick Ways to Close an App

How to Force Quit on a Mac: 5 Quick Ways to Close an App

Use Option–Command–Esc to force quit an unresponsive Mac app, or close it from the Apple menu, Dock, Activity Monitor, or Terminal.

How to Use Split Screen on Mac: Put Two Apps Side by Side

How to Use Split Screen on Mac: Put Two Apps Side by Side

Set up Split View, resize or replace a pane, use Mission Control, and learn when desktop window tiling is a better fit on your Mac.

How to Remove EXIF Data from Photos on macOS

How to Remove EXIF Data from Photos on macOS

Learn how to remove EXIF data from photos on macOS, hide location in Photos, clean exported JPEG copies with ExifTool, and verify the result.

How to Sync Two Folders on a Mac Safely

How to Sync Two Folders on a Mac Safely

Sync two folders on a Mac with SyncTwoFolders, choose a one-way or two-way update, and preview changes before files are replaced or removed.

How to Back Up a Tumblr Blog on a Mac

How to Back Up a Tumblr Blog on a Mac

Back up a Tumblr blog on your Mac with Tumblr’s web export. Download and unpack the ZIP, keep its posts and media safe, and know what the archive leaves out.

How to Use Google Maps on a Mac Desktop

How to Use Google Maps on a Mac Desktop

Use Google Maps on your Mac in Safari or Chrome, find places and routes, switch map layers, and add a Safari web app to your Dock on macOS Sonoma 14 or later.

5 Best Free WAV to MP3 Converters for Windows and macOS

5 Best Free WAV to MP3 Converters for Windows and macOS

Compare five free WAV-to-MP3 converters for Windows and macOS, including online file limits, local batch tools, and practical export settings.