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.
2 Answers
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
-
Thank you for your help nohillside. My question though was how to re-associate sh files with the default macOS application as now I am concerned I broke the operating system? There are other sh files that will not execute any more because of this change.... Just need to revert this modification– User 123Commented Jun 2, 2021 at 15:26
-
1
-
@User123 Not even sure .sh files execute by default. AFAIR I never changed the association for them, they open in Xcode here be default.– nohillside ♦Commented Jun 2, 2021 at 15:41
-
-
RE: "To run shell scripts from Finder you need to" ... give them a
.command
suffix withmv SCRIPT SCRIPT.command
-- To say you need to do this is is just not true. There is no hard requirement for a shell script to have an extension,.command
or otherwise, to run from Finder as long as it's made executable and is associated with Terminal. Commented Jun 2, 2021 at 20:44
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.
.sh
extension? If yes, just associate it with Terminal or remove the extension. If a shell script is set executable and has a proper shebang and no file extension it should automatically open in Terminal. If the shell script did not have an extension when you associated with another application, then just change it's association back to Terminal .