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/
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.
2. On the terminal, type “sudo sh”.
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.
4. The terminal will prompt for the admin password. Enter it and hit Enter again.
5. The script should run fine.
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
2. Next, open the shell script in Finder. Right-click on it and select Open With. From the extended menu, click on Sublime Text.
3. Once the shell script opens in Sublime Text, click on Tools in the top menu. From the dropdown, click on Build.
4. Sublime text will ask for permissions, click on Allow. Next, it’ll show you the result on the bottom split screen.
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.