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
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.
-
4What 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".– fd0Apr 16, 2016 at 14:52 -
3
-
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.– fd0Apr 29, 2016 at 18:41 -
@fd0, I don't understand. How is
sh /path/to/file
any semantically different from the alternative/path/to/file.sh
?– PacerierAug 15, 2017 at 13:36 -
3@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.– fd0Aug 15, 2017 at 19:05
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)
-
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, 2016 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.– rwenz3lApr 27, 2017 at 9: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
-
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 Jul 12, 2019 at 2: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.
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.
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.)