Can I run a Unix command every time I launch the Terminal?
Posted 01/04/2007 at 12:40pm
| by Niko Coucouvanis

You can execute a command every time Terminal launches...

...or open Terminal windows with specific settings. Or both!
Although it's utilitarian by definition and wears its no-frills-ness like a badge of honor, the Terminal has some nifty tricks up its sleeve for those who haven't yet earned the title of Unix Jock. As it turns out, there are a couple of ways to automate Unix commands based on the Terminal's behavior. For starters, you can set a blanket command to execute every time you launch the Terminal application. Just select Terminal > Preferences, check the radio button labeled Execute This Command (Specify Complete Path), and enter a command in the text field. You can type in a simple one, such as uptime (for a report of how long your Mac's been running) or who (to see if anyone is logged in), or you can run a preassembled script by typing in the path to the file.
You can also use similar trickery to attach a command or script to a saved Terminal window, so the command(s) only automate when you open a specific TERM file (that's Terminal-speak for a Terminal Window Settings file). On its own, a TERM file only specifies Terminal windows' settings: font and background color, buffer size, shell emulation, and other geeky goodness that you can set via the Terminal Inspector (File > Show Info). After tweaking a Terminal window's settings to your liking, select File > Save As; in the resulting dialog, supply a name (somename.term), and specify a command or file for Terminal to auto-execute every time you open the file (somefile.term). By default, Terminal saves your TERM files in user name/Library/Application Support/Terminal; double-click a TERM file to launch it, or launch the Terminal and select File > Library to see a list of all the TERM files you've saved.