-->

How to Run Shell Script on Mac

Running a shell script on macOS can be a bit tricky. However, the job gets easier if you keep a couple of basics in mind. So, here’s how to run a shell script on Mac the easy way.

Permissions

Permission is the most important concept while executing a shell script in macOS. Basically, you i.e. the user, should have permission to run the script. It’s weird to tell you that but by default, you don’t have permission to run the script anywhere on the system. It’s disabled due to security reasons.

There are 3 ways to bypass this method:

  • Move the script to your home directory
  • Run the script as a sudo user
  • Provide sufficient permissions to the script

Let’s leave the first option as it can interfere with the functioning of the script. The second option is to run the script as a sudo user. This way, you give the script one-time administrator permission to run. It doesn’t require any setup. You just have to provide your admin password every time you run the script. This is the method I would recommend and if you want to try it, move to this step.

The third method requires giving yourself permission to execute the script. This way you can run the script multiple times without using the sudo command or entering any password. To provide yourself permission to execute, use the following command:

sudo chmod u+x /script-location/
change shell script permission on macos

1. Easy Way

1. The easiest way to run a shell script on Mac is by using the sudo command. To do that, hit Cmd+Space and type Terminal on the Spotlight. When the results appear, hit Enter.

launch the terminal on spotlight

2. On the terminal, type “sudo sh”.

sudo command to run shell script on macOS

3. Next, open Finder. Locate the shell script, drag and drop the shell script from the Finder window to the terminal. The terminal will have the script location. Now, hit Enter.

drag and drop shell script to terminal

4. The terminal will prompt for the admin password. Enter it and hit Enter again.

enter password for sudo command

5. The script should run fine.

sudo command to run shell script on macOS

In case your script ends up with the error – “Permission Denied”, head over to this step to provide appropriate permissions and then try again.

2. Third-party App

It’s better to have a third-party app if you run shell scripts frequently. It will let you edit the script on the fly and run it intuitively. There are multiple shell script editors for mac but the one I prefer is Sublime Text. It’s a minimal markup editor that supports multiple languages. But for the sake of this example, let’s stick to shell scripts.

1. Foremost, download and install Sublime Text.

Download Submile Text for macOS

download sublime text for macOS

2. Next, open the shell script in Finder. Right-click on it and select Open With. From the extended menu, click on Sublime Text.

open shell script in sublime text

3. Once the shell script opens in Sublime Text, click on Tools in the top menu. From the dropdown, click on Build.

build a shell script in sublime text

4. Sublime text will ask for permissions, click on Allow. Next, it’ll show you the result on the bottom split screen.

run shell script on macOS in sublime text

You can edit and save your shell scripts right within Sublime and even build it quickly.

Closing Words: Run Shell Script on Mac

So these were the 2 ways to quickly run a shell script in macOS. In case you frequently edit and deal with shell scripts, it’s better to have a third-party app. The macOS terminal is not intuitive when it comes to shell scripts.

Also Read: Learn How to Use Curl with These Useful Curl Commands

The post How to Run Shell Script on Mac appeared first on TechWiser.

Related Posts

Subscribe Our Newsletter