7

Is it possible to execute a bash script by double-clicking on it? The shell script executes with $ ./script.sh without issues. Double-clicking the script opens TextEdit. It would be nice to execute by clicking on it...

$ system_profiler SPSoftwareDataType 
Software:

System Software Overview:

  System Version: macOS 10.13.4 (17E199)
  Kernel Version: Darwin 17.5.0
  Boot Volume: lily
  Boot Mode: Normal
  Computer Name: lily’s MacBook Air
  User Name: System Administrator (root)
  Secure Virtual Memory: Enabled
1
  • 2
    It's going to be confusing if you say your script is .sh and not .command - you need to have that for click to work out of the box.
    – bmike
    Commented Jun 2, 2018 at 13:24

2 Answers 2

4

Maybe change the extension name to .command, but I think there should be some workaround to make .sh work.

To run by double click on the icon: open terminal and cd to directory of script. then chmod +x . finder will decide if it is excutable or not.

2
  • thanks. as i mentioned, the script runs fine, it has permission to execute
    – lily
    Commented Jun 2, 2018 at 9:15
  • 2
    @lily If you save the script to your desktop and open it with Finder - can you take read/write ownership of it entirely - then double check chmod a+x and drop the icon to the command line window and make sure execute is set? It's likely a permissions issue and putting it on your desktop and using Finder to fix ACL/ownership should clarify if that's the case with where and how you stored the file - not that you don't have .command set properly.
    – bmike
    Commented Jun 2, 2018 at 13:24
4
  1. Go to your Terminal or iTerm.

  2. Change to directory where your file is located.

  3. Type the following:

    chmod a+x (yourscriptname)

  4. Right click on your file and select Open with and Other.

  5. Enable All Applications and choose Terminal.

  6. NOTE: If you always want to open that file with Terminal, then check Always Open With.

  7. Finally, double click on your file and it should work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .