Bash Script that only runs in terminal

bash

I created a Bash script that should be able to be ran on any machine. The problem I'm having it'll only run in terminal. When a user double clicks the script, some users have it open up in iTerm, and some in Xcode in read view. Any idea how to have it execute in these programs or make it only specific to terminal?

Also, I'm having an issue as the script I'm running as its being opened from an "Unidentified developer". I know how to manually resolve from system preferences, but is there anyway to automate in the script?

Best Answer

Add the .command extension

To have a bash script open and execute in macOS's Terminal.app, add the suffix .command to the filename.

If you have a bash script called myscript.sh, rename it to myscript.command. Once renamed, when the file is double-clicked or opened from macOS's Finder the Terminal.app will open and execute the file.

Code Signing

The Unidentified Developer warning appears because the script is not code signed. It is a reasonable warning from macOS, as a script could do harm.

If you are distributing the script through a web site, you need to code sign the payload or contents. How to best achieve this depends on your users' environment.

See making an executable bash file run when clicked for reasonable approaches such as an AppleScript wrapper or platypus.

AppleScript Wrapper

You can embed a shell script within an AppleScript. Exported AppleScripts can be made executable and code signed:

 Applications > Utilities > Script Editor.app

Use the do shell script command and File > Export with code signing enabled.