How do I make this file.sh executable via double click?

BashMacosTerminalExecutable

Bash Problem Overview


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? Also please explain as much as you can, I'm new to Terminal. Thanks.

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

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.

Solution 3 - Bash

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 :)

Solution 4 - Bash

  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

Solution 5 - Bash

you can change the file executable by using chmod like this

chmod 755 file.sh

and use this command for execute

./file.sh

Solution 6 - Bash

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJacobView Question on Stackoverflow
Solution 1 - BashChris PageView Answer on Stackoverflow
Solution 2 - BashNobleUpliftView Answer on Stackoverflow
Solution 3 - BashMarkus Amalthea MagnusonView Answer on Stackoverflow
Solution 4 - BashManishView Answer on Stackoverflow
Solution 5 - BashNight CoderView Answer on Stackoverflow
Solution 6 - BashAamirRView Answer on Stackoverflow