The terminal underpinnings in OS X are the same as those used in Linux. What you are specifically looking for is commonly referred to as "Shell Scripting".
The default shell when you open Terminal.app
(located in /Applications/Utilties
) is the bash shell.
OS X has had a built-in shell handler accessible via Finder and other applications via the .command
extension, though frustratingly not via the .sh
extension. However, setting the script up to run via the Finder is even more complicated still.
- Create a simple shell script, like your example I've made a Hello World application:
#!/bin/bash
echo "Hello World"
Save this file as HelloWorld.command
.
Give this file permission to be executed. chmod u+x HelloWorld.command
should do that.
Open the directory where you saved this file in Finder. Then double click on the HelloWorld.command
file.
This will open up a terminal and execute the script, the output on my computer is the following:
$ /Users/jason/Applications/HelloWorld.command ; exit;
Hello World
logout
[Process completed]
The alternative way of executing this file is while still in the Terminal yourself, simply issue ./HelloWorld.command
assuming you're in the same directory as the file.
Doing so will result in much simpler output:
jason-mac Applications $ ./HelloWorld.command
Hello World
jason-mac Applications $
You've just created and executed a shell script in OS X!