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 Take a Timed Screenshot from Terminal on macOS

How to Take a Timed Screenshot from Terminal on macOS

Set a custom delay for a timed screenshot from Terminal on macOS, then choose its filename, save folder, image format, and pointer settings.

How to Get a Catalina-Style Menu Bar on macOS Big Sur

How to Get a Catalina-Style Menu Bar on macOS Big Sur

Restore a solid Catalina-style menu bar on Big Sur with compatible Boring Old Menu Bar v1.27. Follow the install steps, set light and dark styles, and troubleshoot common issues.

How to Link an Apple ID to a Mac User Account

How to Link an Apple ID to a Mac User Account

Sign in with your Apple ID (now called an Apple Account) in System Settings, then configure the separate Mac login password recovery option for that user.

How to Hide Your Mac From Other Devices on a Network

How to Hide Your Mac From Other Devices on a Network

Turn off Mac sharing services and enable Firewall stealth mode to limit local discovery, with clear notes on what your router and other devices can still see.

How to Check the Mic on a Mac (Built-In or External)

How to Check the Mic on a Mac (Built-In or External)

Learn how to check a Mac microphone using Sound settings and QuickTime, choose the right input, verify permissions, and fix common no-input problems.

How to View CPU, RAM, Disk, and Network Usage in the macOS Menu Bar

How to View CPU, RAM, Disk, and Network Usage in the macOS Menu Bar

Use the free Stats app to show CPU, memory, disk, and network usage in the macOS menu bar, customize widgets, and fix missing icons on macOS 26.

How to Install a Mail Plugin on macOS

How to Install a Mail Plugin on macOS

Install a Mail plugin on macOS using the current Mail Extensions workflow, enable it in Mail settings, and handle older .mailbundle plugins safely.

How to Download YouTube Videos on macOS: Official Methods

How to Download YouTube Videos on macOS: Official Methods

Learn how to download YouTube videos on a Mac with YouTube Premium, save your own uploads as MP4 files, and avoid outdated downloader apps.

How to Download Older Versions of macOS from Apple

How to Download Older Versions of macOS from Apple

Learn how to download older macOS installers from Apple using the App Store, Terminal, or official DMG links, with compatibility and error tips.

How to Toggle the Touch Bar On or Off on macOS

How to Toggle the Touch Bar On or Off on macOS

macOS has no built-in Touch Bar off switch. Learn how to change Touch Bar settings and use Hide My Bar to toggle it off and back on.