macOS - How to execute a shell script with a double clickMost of use use the extension ". sh" for a shell script, but this could be associated with a text editor like BBEdit (highly recommended and there is a free version). So instead of running the script, it will open it in your text editor.
Run Shell script on macOSUse 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. Once you're in the folder, you need to give the script permission to run.
In the Script Editor app on your Mac, click the Run button in the toolbar, or press Command-R, to execute the commands in your script.
First in terminal make the script executable by typing the following command:
chmod a+x yourscriptname
Then, in Finder, right-click your file and select "Open with" and then "Other...".
Here you select the application you want the file to execute into, in this case it would be Terminal. To be able to select terminal you need to switch from "Recommended Applications" to "All Applications". (The Terminal.app application can be found in the Utilities folder)
NOTE that unless you don't want to associate all files with this extension to be run in terminal you should not have "Always Open With" checked.
After clicking OK you should be able to execute you script by simply double-clicking it.
Have you tried using the .command
filename extension?
As of OSX 10.10 (Yosemite) and since at least OS X 10.8 (Mountain Lion), the behavior is as follows when you open (double-click) executable scripts from Finder:
.command
:Preferences... > Profiles
you can opt to automatically close the window when the script exits.cd -- "$(dirname "$BASH_SOURCE")"
right after the shebang linecd -- "$(dirname "$0")"
.chmod +x <script>
in Terminal; otherwise, you'll see the following symptoms:.command
: Finder displays a misleading error message that suggests the problem can be fixed via File > Get Info
, which is not true - use the chmod +x
method suggested above.#!/bin/bash
): behavior is as if the suffix were .sh
- see below..sh
, whether executable or not:TextEdit.app
or, if installed, with Xcode.app
..scpt
or .applescript
(even if they're themselves marked as executable, which is not normally the case):
[Apple]Script Editor
[1] Executable means: a script with the executable permission bit(s) set and the calling user - relative to the ownership to the file - therefore potentially being allowed to execute it.
If you use chmod a+x
to set all permission bits (which is typical), anyone can invoke it (assuming they're also allowed to read the file based on the read permission bit(s) and the file's ownership).
Alternatively, you could create a regular Mac OS X application from your script using Platypus
The easy way is to change the extension to .command
or no extension.
But that will open the Terminal, and you will have to close it. If you don't want to see any output, you can use Automator to create a Mac Application that you can double click, add to the dock, etc.
Automator
applicationRun
button in upper right corner to test it.File > Save
to create the Application.No need to use third-party apps such as Platypus.
Just create an Apple Script with Script Editor and use the command do shell script "shell commands"
for direct command calls or executable shell script files, keep the editable script file safe somewhere then export it to create an Application script. the app script is launch-able by double click or selection in bar folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With