88

I've always wanted to be able to run a script I've downloaded online directly from the Finder but just double clicking the file does not work.

106

Open Terminal, type in sh /path/to/file and press enter.

Faster is to type sh and a space and then drag the file to the window and release the icon anywhere on the window.

  • 3
    What do you think will happen if the shell script is written in another shell 's syntax, such as tcsh, zsh, or ksh? The suffix sh denotes a shell script not "run the script in this shell". – fd0 Apr 16 '16 at 14:52
  • 3
    @fd0 The shebang line will take care of that. – user14492 Apr 29 '16 at 17:13
  • 4
    @user14492- When you invoke a shell script as shell shell_script the shebang line is ignored. The shell reads the first line as a comment. – fd0 Apr 29 '16 at 18:41
  • @fd0, I don't understand. How is sh /path/to/file any semantically different from the alternative /path/to/file.sh? – Pacerier Aug 15 '17 at 13:36
  • 2
    @Pacerier- In the first form, the invoking user must be able to read the file and chooses which interpreter will read the file. In the second form, the file's permissions must be read and execute for the invoking user and the interpreter is determined by the hash bang on the first line of the script. In both cases the kernel does not interpret the suffix on the file. – fd0 Aug 15 '17 at 19:05
12

Alternatively, you could also do

cd /directory/with/executable
chmod +x executable     # only required if your file is not already executable
./executable

which will also run the executable file with its specified shell (if specified in the shebang #!/bin/(shell)

  • 1
    In the question it says "run a script I've downloaded online". Files downloaded from the Internet usually have permissions set to 644. – nohillside Apr 16 '16 at 17:32
  • @patrix i think it's not just internet, but every file that is copied from another filesystem onto yours because of the default mask in your account. – rwenz3l Apr 27 '17 at 9:09
9
chmod u+x myfile.sh
cp myfile.sh /usr/local/bin

edit ~/.bash_profile and add the following line:

alias myfile=./myfile.sh

execute the following command-line:

source ~/.bash_profile

then you will be able to run your file as a program

$ myfile

  • in Ubuntu any script in your path can be executed as long as you have #~/bin/sh, while in OSX the script should have .sh extension and an alias for each one of them. :-O – Fabrizio Bertoglio Jul 12 at 2:36
6

Follow these steps to run the script files:

  1. Right-click on the .sh file.

  2. Hover over Open With.

  3. Choose Other....

  4. You should be in the Applications folder. Open Utilities folder and select Terminal.app.

  5. If you can't select Terminal.app, change the enabled applications from Recommended Applications to All Applications. It is at the bottom of the window.

  6. If you want to open every .sh file with Terminal.app, tick Always Open With.

  7. Press the Open button in the bottom right corner of the window.

3

The reason it doesn't work after downloading is the file permissions don't allow it. To enable execute permissions, open Terminal and type chmod 755 /path/to/script. Instead of typing the full path, you can drag the script onto the Terminal window from Finder. Then, to execute, just enter /path/to/script. Again, you can drag and drop the file onto the Terminal window. This syntax should execute the script using the correct shell as defined on the first line of the script.

  • My file .sh opens in the text editor I created the file with. How can I open it in Terminal instead? I've set the right permissions. – bart Jan 19 '17 at 18:23
  • @bart you need to add the .command extension for executing a script within finder directly. – rwenz3l Apr 27 '17 at 9:08

You must log in to answer this question.

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