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.
6 Answers
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)

- 113

- 3,761
-
1In 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 09:09
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

- 51,809

- 319
-
in Ubuntu any script in your path can be executed as long as you have
#~/bin/sh
, while inOSX
the script should have.sh
extension and analias
for each one of them. :-O – Fabrizio Bertoglio Jul 12 '19 at 02:36
Follow these steps to run the script files:
Right-click on the .sh file.
Hover over Open With.
Choose Other....
You should be in the Applications folder. Open Utilities folder and select Terminal.app.
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.
If you want to open every
.sh
file with Terminal.app, tick Always Open With.Press the Open button in the bottom right corner of the window.

- 51,809

- 245
OP's question was how to run the script in the Finder. None of the Unix solutions above do this. The answer is to set the file's extension to .command, eg, script.command. Double-clicking it will start Terminal and execute the script. (Make sure permissions are set to be executable.)

- 89
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.

- 2,969
-
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 09:08
sh
denotes a shell script not "run the script in this shell". – fd0 Apr 16 '16 at 14:52shell shell_script
the shebang line is ignored. The shell reads the first line as a comment. – fd0 Apr 29 '16 at 18:41sh /path/to/file
any semantically different from the alternative/path/to/file.sh
? – Pacerier Aug 15 '17 at 13:36