Associate sh files on Mac with correct application
I am new to macOS. I created an sh file but associated it with an editor in order to modify it. But now it opens with the editor and does not execute any more. Please advise how to associate sh files with correct application as before the change.
Solution 1:
To run shell scripts from Finder you need to
- make them executable with
chmod +x SCRIPT
- give them a
.command
suffix withmv SCRIPT SCRIPT.command
To keep them as .sh files
- select one of them in Finder
- open the Info window (Cmd-I)
- change the associated application to Terminal.app
Solution 2:
As an example, testing on a clean install of macOS Big Sur 11.4 (Intel) with no additional software installed and only the default installed software, a shell script made executable with a proper shebang, without an extension will by default be opened by and run in Terminal when double-clicked in Finder. One with a .sh
extension will open by default in TextEdit.
Note that without a proper shebang it may not execute properly by itself.
A shell script with or without an extension can be associated with a different application, either individually or globally.
If you have associated your shell script with an application other then Terminal, or its default application and you want to associate it back to Terminal, or its default application, simply do it the same way you did it to begin with.
To change the association of a file in Finder, simple select the file and press ⌘I, then on its info sheet change its Open with: application setting.
Additional Information
Installs of some software, Apple and or a third-party application, can change the association of a shell script with a .sh
extension to something other then TextEdit. For example, installing Xcode will associate shell scripts with an .sh
extension to it, taking it away from TextEdit.
A user can also change the default association at will.
For a global association change you can click the [Change All…] button, although do not do this if your are not sure of the action you are taking.
As a general rule, shell scripts should be run directly in Terminal and not from Finder. Opening for edit can be done easily in Terminal or Finder.