1. Home
  2. Mac OS X
  3. Run shell sh script on macos

How to run a Shell or .sh script on macOS

A shell script is a computer program or text file made of several UNIX commands. It saves you the time of inputting these one by one into the command line. This means you can automate certain simple software operations, such as searching for directory files, editing and sorting data, and other basic actions. It’s a huge time saver, converting text-based commands into actions for the operating system.

In order to run a shell script, you’ll need to enter text in a specific manner. We’ll cover some basics.

What to Know about Shell Scripts

You’re not going to fully learn everything about shell scripts from a single article. Learning a scripting language takes a little time and practice. They even have courses and shell script resources available if you want to learn all the ins and outs. That said, we can get you pointed in the right direction. 

What you need to start: 

  1. A text editor compatible with macOS, like TexEdit, Visual Studio Code, BBEdit, etc. These editors help with flagging errors, autocompletion, code formatting, or even running the script via the editor for testing, depending on the one you’re using. 
  2. A shell. This is the command-line interpreter that will be processing the actual script. The default shell for mac used to be bash. Now, it is zsh (zshell). Either one works and there are pros and cons to each depending on your preferences.
  3. Familiarity with shell commands. There are several common commands, such as cp: copy files, or killall: stop a running process or app. It’s easy to find resources to help you learn all the different types available.  
  4. Be aware that many bash shell scripts begin with the characters # and !. These are referred to as a “shebang.” This identifies that it’s a shell script so you’ll be using them a lot, especially if you’re using bash based commands, which are also compatible with zshell.

Test Shell script

If you want to test a shell script, you can use one you have on hand, or the sample script below. Open a text editor e.g., TextEdit and paste the text below into it. It’s important to save the file with the .sh file extension.

#!/bin/bash
echo "Stay Home"

Run Shell script on macOS

Open the Terminal on macOS. Use the cd command to move to the folder that the script is in. The location in the command below is just an example. Make sure you change it to whichever folder your script is actually in.

cd ~/Downloads

Once you’re in the folder, you need to give the script permission to run. Use the command below but replace the example name with the actual name of the script that you want to give permission to run to.

chmod +x test_script.sh

After granting the script permission to run, use the command below to run it. Remember to change the name of the script before you run the command.

sh test_script.sh

Why shell scripts are easier to run on macOS

Shell scripts are easier to run on macOS compared to Windows because macOS is based on UNIX, much like a Linux system. Shell commands will run easily and you’ll be able to use a few select Linux apps too, though macOS versions are preferable when available.

The test script we gave will simply display text in the Terminal but the script you want to run is going to do something else and may need other apps/utilities installed on your Mac before it can run. If you’re skilled, you can modify the script and instruct it to use apps or utilities already installed on your Mac.

1 Comment

  1. >Shell scripts are easier to run on macOS compared to Windows 10 because macOS is based on UNIX.
    lol easier to run. how do I run the .sh files from within finder.
    why would I want to navigate in terminal to open a script. lol