1. Home
  2. Mac OS X
  3. Run terminal commands from a script on macos

How to run Terminal commands from a script on macOS

Terminal commands on macOS aren’t difficult to run. The only difficult or inconvenient part is where you have to type the command out. If you often have to run a Terminal command and you’re tired of having to type it out over and over again, it’s a good idea to just save it as a script and run it instead. Here’s how you run Terminal commands from a script on macOS.

Creating script

You can create the script in any text editor. macOS comes with a built-in text editor in the form of TextEdit. Open a new text file and enter all the commands that you want to run. Once you’ve entered the commands, save the file with the COMMAND file extension. Give it a name that will tell you what the script is for e.g., ScreenshotScript.command.

Save it anywhere you like.

Give the script permission

Open Terminal and run the following command. Replace ‘path to script’ with the actual path to the script you just created. You can get the path to the script by right-clicking it, holding down the Option key, and selecting the copy path option from the context menu.

Syntax

chmod u+x path-to-script

Example

chmod u+x /Users/Fatimawahab/Desktop/MyScript.command

That’s all you need to do. When you double-click the script file, it will open a Terminal window, and run all the commands in the script. You may see a prompt requesting certain permissions the first time you run the script so it’s a good idea to give it a test run before you make the script a part of your daily workflow.

The permission you grant the script is on a per-script basis. It is subject to the script and not the Terminal. What this means is that for each Terminal command you package into a script, you will have to give it permission before you can double-click it to run it. If you don’t, the script won’t run and the error message you get won’t be able to guide you much on what you need to fix. Make sure you do not miss this step.

There are other ways to run a Terminal command without having to type it out each time but a script is a flexible method to do it. If you share your script with someone else, they too will need to allow it permission to run from the Terminal before they can use it. The permissions are not inherited from one system to another when you share the script file.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.