If you are here, you are likely interested in learning to use Shell scripts on your Mac. Whether you are new to Shell entirely, or familiar with Shell due to experience with UNIX or Linux operating systems, this is the place for you! 

In general, this article will not cover how to write shell or .sh Scripts, but we will give you a few tips on how to write them and what to look out for.

Key Takeaways

  • Shell or .sh scripts are plain text files that contain one or more Terminal commands to execute tasks that would otherwise use Terminal. 
  • Before macOS Catalina, the default script was Bash Scripts. But macOS Catalina changed this default to zsh. This is important information, but does NOT restrict you from using Shell
  • While Mac does have a text editor to edit .sh files, there are free alternatives that may be better for more advanced scripts or beginners.
  • Once the Shell or .sh file is created, it is a simple 3 commands to run it!

What is a Shell or .sh Script?

First off, if you are less familiar with Shell (or .sh) scripts, they are plain text files that contain one or more UNIX commands (or Terminal commands) that can be used to execute commands that you would otherwise use the Terminal to execute. 

The files are followed by the .sh suffix. Since macOS is based on UNIX operating systems, this makes it relatively easy to write and execute Shell scripts on your Mac. 

Up until macOS Catalina, macOS used Bash scripts as their default, but Catalina changed the default to zsh. This, however, is just the default and does not change the other options for the terminal. You can still use whichever script you are most comfortable with.

Basics of Writing a Shell or .sh Script

Shell scripts or .sh files are typically written in text editors. The Text Edit application is the default plain text editor for Mac, but it is not necessarily the best for script creation, especially if you are not familiar with script writing. 

If you plan on writing scripts for your Mac more regularly or are just starting and trying to write a more advanced script, it may be worthwhile to find and download an additional editor. There are also specific scriptwriting applications that are great for beginners or advanced scripts!

The easiest way to create a shell file on Mac is using the Terminal commands:

Cd YourFolder
touch CreateFileName.sh” 

Additionally, Shell scripts always begin with a binary path to allow your Terminal to recognize that it is a Shell file. 

It begins with what is called a shebang. This is the hashtag and exclamation point. Then, it gives bin, which stands for binary, and sh for shell. 

So it will look like this: #!/bin/sh.

For our purposes, I will be using the following Shell file. This is located in my documents folder.

Running the Shell or .sh Script

So, with the Terminal app on your Mac, we will use a series of commands to run the shell file. They will look like this:

The first command is to bring your terminal to the folder with your Shell script. As I stated above, mine is located in my Documents folder. 

cd YourFilePath

The next command changes the permissions for the file to allow you to run it. 

chmod 755 YourFileName.sh

Lastly, the command allows you to run the file.

sh YourFileName.sh

Additional Run Commands

There are a few additional commands that are slightly more complex and allow you to run the file in different ways or for different reasons. All of the following commands and their output are shown here:

If your .sh file requires special permissions, use the command:

sudo sh YourFileName.sh

Instead of sh, you can use the command ./YourFileName.sh. This allows your Mac to know to read the shebang and use the correct Script type.

Lastly, you can use the following commands:

  • sh -x YourFileName.sh for debug mode
  • sh -v YourFileName.sh for verbose mode
  • sh -n YourFileName.sh to check the syntax

Final Words

Believe it or not, running a Shell or .sh script on your Mac is as simple as the 3 commands we discussed! Though there are several other ways to run the script (some are discussed), these commands are very easy to remember and execute. 

If you are familiar with Unix or Linux, you will have a very easy time picking up using Shell scripts on macOS as well. It is also a great way for newer Shell users to learn!