Write and run scripts through BBEdit and TextWrangler

September 4th, 2013 by William Smith

Part of my job is writing shell scripts. These are scripts for administrative work rather than end-user applications and they’re usually short and non-interactive. I can’t justify purchasing expensive code-writing tools for this type and frequency of work but I do prefer something more than just TextEdit. I could write a website using TextEdit but that would be painful. The same applies to scripts.

Two of my favorite script-writing tools are from Bare Bones Software: BBEdit and TextWrangler. These are actually text editors but that’s really all that’s needed for basic scripting. I recommend TextWrangler because it’s free and really powerful. For those who want more I recommend purchasing BBEdit, which is the big brother to TextWrangler.

Here’s just one thing I like about each when writing scripts.

TextWrangler

Part of script writing is testing the code. Sometimes I’m just writing a snippet and only need to test a line or two. TextWrangler (and BBEdit) include a shebang (#!) menu to let me run code from the text I’ve just typed.

I can open a new TextWrangler document and enter a simple script to tell me today’s date:

#!/bin/sh
	date "+Today's date is %m/%d/%Y"
exit 0

To run this script I don’t even need to save the document. I can just choose #! > Run:

Sheband > Run

and the result opens in a new window.

Shebang > Run output

If my script syntax were incorrect, such as omitting the final double-quote on the second line, the result would the same as if I had saved the file, made it executable and run it in Terminal.

Shebang > Run error

BBEdit

BBEdit has a feature called Shell Worksheets, which act kind of like an interactive script. I can create a new worksheet by choosing File menu > New > Shell Worksheet.

A new shell worksheet is based on the default UNIX shell, which is generally bash. It doesn’t require a shebang at the beginning of my code. I can enter my date command and then press Enter (on an extended keyboard) or Command-Return and that one line is not only executed but the result is displayed below.

Shell worksheet

If I have several lines of code I can highlight any one or multiple lines and press Enter or Command-Return to execute those lines. All output will appear after the last line of highlighted commands.

Better than TextEdit

Working within TextWrangler or BBEdit enables me to write and quickly test code without having to save my script and make it executable. In addition to quickly executing commands both applications feature line numbering and syntax highlighting to make reading and debugging scripts much easier.

For a better understanding of these tools consult the User Manual under the Help menu in each application.

Tags: ,

Comments are closed.