I have a shell script that has user execution permission on OS X, but when I double click on it, it opens in a text editor. How can I get it to run by double-clicking it?
8 Answers
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.
-
thanks i got it, I didn't know to associate with terminal, I thought need to associate with /bin/sh – c2h2 Feb 26 '11 at 09:21
-
31I couldn't find *Terminal.app* in the list until I realized that I had to look in the "Utilities" folder. Hopefully this will save somebody some time. – Glenn Lawrence Mar 13 '15 at 04:17
-
45On OSX 10.8 and higher, an executable script whose names has either _no_ suffix or suffix `.command` is _by default_ executable from Finder, without the need to instruct Finder how to open it. – mklement0 Jul 08 '15 at 18:24
-
Terminal.app was greyed out as a possibility even after I chmod'ed it. – Cheruvim Feb 16 '17 at 21:46
-
1"All Applications" is the setting I was missing...probably what Cheruvim experienced as well. As for Glenn's comment: you can simply use the build-in Search feature at the upper-right corner of the window; make sure you type in the full name of the app, i.e. Terminal.app. – elder elder Mar 02 '17 at 13:25
-
Thank you so much for adding the details on how to get it to run with Terminal. I wasted 30-40mins on it before I searched stackoverflow :( – AJC Jun 13 '17 at 18:00
-
@mklement0, On OSX 10.12.5 (Sierra, clean install) with the ``.command`` suffix, I needed to ``chmod a+x`` to make the script executable in the Finder, otherwise permission denied (I had read and write privileges). That was a ``Miniconda3-latest-MacOSX-x86_64.sh`` with extension changed to ``.command``. – PatrickT Jul 09 '17 at 19:15
-
@PatrickT: Yes, that's why I said (emphasis added): "an _executable_ script ...". _Executable_ means: a script with the executable bit(s) set and the calling user - relative to the ownership to the file - therefore possibly being allowed to invoke it. If you use `chmod a+x` (which is typical), _anyone_ can invoke it (assuming they're also allowed to _read_ the file). In other words: my comment wasn't about `chmod a+x`, it was about not needing to _also_ tell Finder how to open such scripts. – mklement0 Jul 09 '17 at 19:21
-
9Not the clean way to do it. Rename your script to the `.command` file extension. – Bachsau Apr 16 '18 at 18:56
-
1This works, but it always brings my Terminal window to the front. Is there any way to do this without the Terminal window being brought to the front? Maybe a way to have the script backgrounded from the start? – Tango Jan 30 '19 at 15:26
Have you tried using the .command
filename extension?

- 16,440
- 5
- 29
- 37
-
3This is better. In the @Lus answer the working folder isn't the same you open the script. – Rodrigo Jan 26 '12 at 14:41
-
9
-
8Please note, that current directory may not be the one your script located in. Take a look at this question to set it right: http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in – Pavel Alexeev Aug 27 '13 at 16:42
-
22You still need to have execution permission (`chmod +x`) but the `.command` extension is already linked with Terminal. Great solution, thank you – NorTicUs May 22 '14 at 09:56
-
1Apparently the user can't interact with the terminal program with this method - unless I am mistaken? – Demis Jun 01 '16 at 00:31
-
1
-
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:
Executable scripts[1] with either NO suffix or suffix
.command
:are executed by default - no setup required:
- a new Terminal window opens in which the script runs.
- by default, the window will remain open after the script terminates so you can inspect the output (though at that point the shell that ran the script has exited and you cannot interact with it any longer).
However, via Terminal'sPreferences... > Profiles
you can opt to automatically close the window when the script exits.
Caveat: the working folder is invariably the current user's home folder, NOT the folder in which the script is located.
- To make a shell script change to the folder in which it is located, place
cd -- "$(dirname "$BASH_SOURCE")"
right after the shebang line- or, if you must remain POSIX-compliant,
cd -- "$(dirname "$0")"
. - For edge cases, such as finding a symlinked script's true source directory, see this answer.
- To make a shell script change to the folder in which it is located, place
If the script is unexpectedly not executable:
Make it executable by running
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 viaFile > Get Info
, which is not true - use thechmod +x
method suggested above.no suffix:
- with a shebang line (e.g.,
#!/bin/bash
): behavior is as if the suffix were.sh
- see below. - with no shebang line: opens in your default text editor (which is TextEdit by default).
- with a shebang line (e.g.,
Scripts with suffix
.sh
, whether executable or not:- are opened for editing in
TextEdit.app
or, if installed, withXcode.app
.
- are opened for editing in
Scripts with suffix
.scpt
or.applescript
(even if they're themselves marked as executable, which is not normally the case):- opened for editing in
[Apple]Script Editor
- Note that the JXA source-code files seem to have no distinct suffix (yet).
- opened for editing in
Scripts with a custom suffix (a suffix not yet known to the system), whether executable or not (in fact, applies to any kind of file):
- prompt you for the app to open them with when you first open them, and remember that choice.
[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).

- 382,024
- 64
- 607
- 775
-
1It's possible to close the opened Terminal window when the script exits with a simple `osascript` you can include in within the `.command` file instead of manually messing with the profile. – l'L'l Jan 12 '16 at 20:43
-
@I'L'I: Do you mean adding a `osascript -e '...'` command to the end of one's script? What's the specific command, and does it ensure that the right tab is closed even when the script is not running in the frontmost tab? – mklement0 Jan 12 '16 at 21:01
-
5@mklement0: Yes, I haven't tested it more than a few minutes, but the basic idea is to include on the last line of the **.command** script: `osascript -e 'tell application "Terminal" to close front window' > /dev/null 2>&1 &` ... It might not work in every scenario, although the redirect at the end is really the key — the osascript can otherwise be adapted easily (eg. **window** may need to be changed to **tab** if that is your terminal default behavior, etc.). – l'L'l Jan 13 '16 at 01:12
-
2Is it possible to not let the Terminal window/tab close after `.command` script file has been ran despite the profile setting? – CyberMew Apr 02 '18 at 05:19
-
@CyberMew: If you can modify the script, add a command that waits for a keystroke at the end; e.g., if it's a `bash` script, add `read -p 'Press Return to close this window.'` as the last statement. – mklement0 Apr 02 '18 at 12:25
-
Is there a way to make the window of the `.command` not to close? I mean I want to create a `.command` file which when executed opens a terminal with pre defined `PATH` variable. – Royi Sep 01 '18 at 16:00
-
@Royi: In your `.command` file, first set up `PATH` as desired, then invoke `bash` - without arguments - to start an interactive session that will keep the window open. – mklement0 Sep 01 '18 at 16:12
-
@Royi: Make sure that your `.command` file has a valid shebang line (e.g., `#!/bin/bash`); if it still doesn't work, please create a _new question_ with an [MCVE (Minimal, Complete, and Verifiable Example)](http://stackoverflow.com/help/mcve); feel free to ping me here once you have done so. – mklement0 Sep 01 '18 at 16:26
-
@mklement0, Have a look at https://apple.stackexchange.com/questions/335608. Thank You. – Royi Sep 04 '18 at 19:40
Alternatively, you could create a regular Mac OS X application from your script using Platypus

- 1,303
- 1
- 14
- 23
-
This is a really great app. But I have some problem here. I have a python script that I want to convert into an .app file. My script includes a line where the user has to type some input (raw_input()), when the .app reaches this line of code, it throws an EOF (end of file) error. What can I do about it? – Apr 03 '13 at 22:25
-
1It's not an interactive terminal. Platypus merely presents script output. There's no bidirectional communication. – svth Jul 18 '13 at 23:38
-
5Or create an OS X application with Automator using the Run Shell Script action. – Rangi Keen Jul 04 '15 at 03:38
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.
- Open
Automator
application - Choose "Application" type
- Type "run" in the Actions search box
- Double click "Run Shell Script"
- Click the
Run
button in upper right corner to test it. File > Save
to create the Application.

- 33,218
- 10
- 150
- 101
-
-
or you can do 3rd step in this article after changing the extension to .commandhttps://medium.com/@nooneypradeep/double-click-to-execute-a-shell-script-in-mac-os-monterey-3-steps-2f6f80939f48 – Pradeep Nooney Jan 08 '22 at 09:56
-
Make sure you change the file format to "Application"; otherwise you'll get a "Workflow" which will just open up Automator. – Brad Turek Feb 14 '22 at 19:11
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.

- 382,024
- 64
- 607
- 775

- 3,715
- 2
- 29
- 29
-
4Even better, use Automator and use the Run Shell Script action directly rather than going through Apple Script. – Rangi Keen Jul 04 '15 at 03:36
You can also set defaults by file extension using RCDefaultApp:
http://www.rubicode.com/Software/RCDefaultApp/
potentially you could set .sh to open in iTerm/Terminal etc. it would need user execute permissions, eg
chmod u+x filename.sh

- 1,665
- 1
- 13
- 15
chmod 774 filename
Note: The file with name 'filename' that has the bash script has no extension

- 194
- 2
- 8