Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

In Windows, I can create .bat files. I double click it and all lines are interpreted as if I had entered it into the console.

In OS X, what file or program do I have to use? If I want write a file with;

 echo hello world

I want to double click this file and have it execute in a console. How I do this?

share|improve this question
    
does the file need to run when double-clicked, or do you just want to run the file by typing the file name? –  Phil M Aug 4 '11 at 19:43
    
    
double-clicked file is more fast :) –  Rodrigo Aug 4 '11 at 20:38

6 Answers 6

up vote 8 down vote accepted

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.

  1. Create a simple shell script, like your example I've made a Hello World application:

    #!/bin/bash
    echo "Hello World"

  2. Save this file as HelloWorld.command.

  3. Give this file permission to be executed. chmod u+x HelloWorld.command should do that.

  4. 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!

share|improve this answer
    
As an aside: It's ridiculously frustrating that OS X doesn't do this with ".sh" files, only ".command" files. Yes, it's possible to change this, but .command already exists, so this is my suggested solution. –  Jason Salaz Aug 4 '11 at 19:24
    
Do you want to ask the question how to inject .sh into the launch services database or shall I ? –  bmike Aug 4 '11 at 20:07
    
You obviously had the idea (the thought didn't even cross my mind), so you're welcome to do it. –  Jason Salaz Aug 4 '11 at 20:11
    
I got the idea from you - here goes, ima ask it all on it's own... –  bmike Aug 4 '11 at 20:21
    
It give-me: chown: u+x: Invalid argument –  Rodrigo Aug 4 '11 at 20:45

The equivalent of the Console or Command Prompt in Windows is the Terminal app located in /Applications/Utilities/

The equivalent of DOS (or whatever they're calling it these days) is "bash"

The equivalent of a batch file (.bat) in Windows is a shell script (.sh)

Just do a Google search for "bash" "reference" and you'll figure the rest out. I would also recommend buying the Bash Cookbook (O'Reilly).

Specifically, this would be what you would type into a terminal window to get your "hello world" script:

To create the file:

    echo "echo hello world" > file.sh

To change permissions to make the file executable:

    chmod 755 file.sh

To run the script:

    ./file.sh

If you want to be able to double-click it from the Finder, right-click (or control-click) on the file and select Get Info, then under Open With, select the Terminal app. Or you could use the .command extension instead of the .sh extension.

share|improve this answer

You probably want to give AppleScript a look. This book by Sal Soghoian is considered by many the go-to book on AppleScript.

Another nice way for users to automate Mac apps is using Automator. You may want to give it a try as well. Hope this helps.

Update

It seems that you're looking for something like Unix shell scripting. This two tutorials are decent and seem to cover the basics.

share|improve this answer
    
I saw this, but the AppleScript isn't the terminal script. I think you can do: "Go to terminal and write x command". Well, isn't the .bat style (and little more difficult) –  Rodrigo Aug 4 '11 at 20:35
    
It seems like Unix shell scripts is what you're looking for. –  Christian Correa Aug 4 '11 at 21:11

Shell scripts. I don't think Ask Different would be the suitable place to discuss it in depth but there's a very detailed guide here: http://tldp.org/LDP/abs/html/index.html

Mac OS X is UNIX Certified from 10.5 on (right?) so this wisdom here is also applicable to Linux and other UNIX-like OSs.

(in a less complex way, Mac OS X's Automator is pretty damn powerful, as is the AppleScript Editor, both of which can execute shell scripts and other commands with programs, and wrap them up into a convenient .app bundle for Launchpad or sharing with friends)

share|improve this answer
    
From the introduction of the guide I linked: "A shell program, called a script, is an easy-to-use tool for building applications by "gluing together" system calls, tools, utilities, and compiled binaries. Virtually the entire repertoire of UNIX commands, utilities, and tools is available for invocation by a shell script. If that were not enough, internal shell commands, such as testing and loop constructs, lend additional power and flexibility to scripts." –  Swizzlr Aug 4 '11 at 19:12
    
And by "suitable place" I think shell scripting is really best discussed over on SuperUser: superuser.com/questions/tagged/shell-script –  Swizzlr Aug 4 '11 at 19:17

I made a video a couple of days ago explaining this. Hope this helps: http://www.youtube.com/watch?v=Q2jxpJAlsyg

share|improve this answer
    
Could you please explain it here not just provide a link –  Mark Jul 30 '13 at 19:40

For mac is the .sh file aka Shell script

share|improve this answer
    
But when calling it from the finder, you will want to use .command. .sh is an unhandled filetype from the finder (by default). –  Jason Salaz Aug 4 '11 at 19:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.