203

First off I'm using Mac.

Next, I need to execute this "file.sh" we will call it. Everytime I need to execute it I have to open Terminal and type:

cd /Users/Jacob/Documents/folderWithFileInIt
bash file.sh

This is okay, but I feel like it would be a lot quicker if I make the file execute on double click, don't you think?

So my question is, how do I make this file executable via double click?

My ideas were either:

a) type something like chmod into terminal and change permissions?

b) make a file, put code I wrote above in it ^ and then make that file executable?

c) make an automation somehow to do this?

Which way is best, or is there an even better way?

starball
  • 20,030
  • 7
  • 43
  • 238
Jacob
  • 3,835
  • 8
  • 25
  • 25
  • 1
    If you want a nice, easy to click script, then yes, Automator is the right tool. You seem to know that from your question, so what is your question? – Rob Napier Dec 07 '11 at 03:26
  • same Question but i have jdk dmg ,some app folder for creating icons ,some Jar ..but i am failed to run/create via automator or .command – SANTOSH Jul 18 '19 at 07:29

7 Answers7

411

By default, *.sh files are opened in a text editor (Xcode or TextEdit). To create a shell script that will execute in Terminal when you open it, name it with the “command” extension, e.g., file.command. By default, these are sent to Terminal, which will execute the file as a shell script.

You will also need to ensure the file is executable, e.g.:

chmod +x file.command

Without this, Terminal will refuse to execute it.

Note that the script does not have to begin with a #! prefix in this specific scenario, because Terminal specifically arranges to execute it with your default shell. (Of course, you can add a #! line if you want to customize which shell is used or if you want to ensure that you can execute it from the command line while using a different shell.)

Also note that Terminal executes the shell script without changing the working directory. You’ll need to begin your script with a cd command if you actually need it to run with a particular working directory. E.g. you can use cd "$(dirname "$0")" to set the current working directory to the directory where your shell script lies.

malhal
  • 26,330
  • 7
  • 115
  • 133
Chris Page
  • 18,263
  • 4
  • 39
  • 47
  • 51
    Also you can use `cd "\`dirname "$0"\`"` to set the current working directory to the directory where your shell skript lies. – gearsdigital Jun 23 '13 at 21:14
  • 3
    For aesthetically reasons you should also right click on the .command file and open the infos. There you have the option to hide the suffix (under name & suffix). – Bijan Dec 11 '13 at 15:40
  • I use this method to run apps via `open -n ...` command. It works, but every time Terminal window opens and and shows me "[Process completed]". Is it possible to prevent Terminal window appearance somehow? – Nik May 24 '15 at 14:11
  • @dig I recommend that you post that as a question. – Chris Page May 24 '15 at 18:00
  • @BenC.R.Leggiero: You didn't make it executable with `chmod`. – Chris Page Nov 11 '15 at 19:39
  • @ChrisPage well, I did that before renaming it and upping the permissions on it (so I and everyone else has read/write). That must've wiped it out somehow. – Ky - Nov 11 '15 at 22:14
  • @Bijan this must have changed. On High sierra, you can't change suffix without making the command un-runnable – 1mike12 Dec 15 '17 at 15:13
  • @Nik did you get any info on how to prevent the terminal window getting open? :) – abyin007 Apr 15 '19 at 11:52
  • @abyin007 no :( – Nik Apr 16 '19 at 12:09
  • 1
    @Nik There’s no point of running it in Terminal if you don’t want to it to show up in a terminal window. Instead, make it an application bundle whose executable is a shebang script. Or make an AppleScript or Automator applet that runs the script. You can also use Terminal Preferences to change the profile to close the window when the process exits, but that’s not the best solution if you just want to run a shell script without user interaction. – Chris Page May 08 '19 at 13:14
34

Remove the extension altogether and then double-click it. Most system shell scripts are like this. As long as it has a shebang it will work.

NobleUplift
  • 5,631
  • 8
  • 45
  • 87
13

You can just tell Finder to open the .sh file in Terminal:

  1. Select the file
  2. Get Info (cmd-i) on it
  3. In the "Open with" section, choose "Other…" in the popup menu
  4. Choose Terminal as the application

This will have the exact same effect as renaming it to .command except… you don't have to rename it :)

Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
9
  1. Launch Terminal
  2. Type -> nano fileName
  3. Paste Batch file content and save it
  4. Type -> chmod +x fileName
  5. It will create exe file now you can double click and it.

File name should in under double quotes. Since i am using Mac->In my case content of batch file is

cd /Users/yourName/Documents/SeleniumServer

java -jar selenium-server-standalone-3.3.1.jar -role hub

It will work for sure

Manish
  • 1,085
  • 9
  • 5
1

you can change the file executable by using chmod like this

chmod 755 file.sh

and use this command for execute

./file.sh
Night Coder
  • 45
  • 2
  • 6
  • 2
    Welcome to SO! Your answer does not add anything to the already accepted answer. Please explain why is this different from the other answers that already exist. – GabrielOshiro Jul 14 '20 at 20:53
0
nano ~/FILENAME

Write your bash script and exit nano with Ctrl + x and hit y

Make file an executable

chmod 700 ~/FILENAME

Bingo, the file turns an executable, double click to launch

Works without .sh extension or shebang (#!) prefix.

AamirR
  • 11,672
  • 4
  • 59
  • 73
0

If you give the script file the extension .app then you can run it with double click.

malhal
  • 26,330
  • 7
  • 115
  • 133