Update 2005-02-15: Now contains information for setting aliases in both tcsh and bash.
Shell? What shell?
Before using this you'll need to know which shell you're running. Originally Apple included tcsh as the default but nowadays they include bash. To find out which one you've got type this into the Terminal (case sensitive):
[bob-adamantium:~] bob% echo $SHELL
If you see tcsh in the response you're using tcsh and you should follow the orangish blocks. If you see bash in the response you're using bash and you should follow the greenish blocks below.
And now: the installation
When I first went looking for a calculator in the command line I just automatically typed in calc because, if I were going to make a command-line calculator, that's what I'd call it. But that failed and alas,it was not to be. Not to be, that is, until anonymous over on FA: OSX pointed out that there's a perl-based calculator script called 'calc', aka: the Astronomy-aware Unix calculator.
'calc' is exactly what I was looking for.
Those readers intimately familiar with the command line have likely already clicked that link, downloaded the script, installed it and discovered that you can automatically get the speed of light by entering:
[bob-adamantium:~] bob% calc c
at the command prompt. Cool, no?
Readers not so familiar with the command prompt have probably already stopped reading this article but just in case you're still here I'm going to tell you how to install and use 'calc', a truly painless process despite involving the command line. No, really. Honest.
First click on Astronomy-aware Unix calculator and download the file linked via the "Perl Script" link to your computer. To download it instead of viewing it in your browser hold down the Control key when you click on the link and select "Save Linked File As...". This saves it as a file called 'calc-src' to your hard-drive, which is a terrible name for our purposes but we'll worry about that later.
Second find the file you just downloaded in the Finder and move it somewhere you'll be able to easily find it again later. In my case I created a folder called 'utilityscripts' in my Documents folder to house these sorts of things, thus I end up with:
/Users/bob/Documents/utilityscripts/calc-src
Third rename the file from 'calc-src' to 'calc.pl'. We're changing its name for two reasons:
- it tells us this is a perl script by sight (the .pl part)
- we're going to alias it so we can launch it by typing
calcat the command prompt so it makes sense that the file have that name too (the calc part)
As this stage you've downloaded the file, moved it somewhere that you can easily find it again, and you've renamed it to something reasonable. Things are going well, yes?
Good. Time to fire up the Terminal (which is located in your /Applications/Utilities/ directory). Launching Terminal opens a new blank window with something like the following in it (but not exactly like this since this is mine and not yours and your username and machine name will differ):
Last login: Mon Oct 18 10:40:42 on ttyp1
Welcome to Darwin!
[bob-adamantium:~] bob%
Fourth we now need to set the permissions on this file so we can execute it. At the command prompt type the following: chmod 755 so things now look like (but don't press the Return key yet):
[bob-adamantium:~] bob% chmod 755
Next, select the calc.pl file in your Finder window and drag it onto your Terminal window. This automatically pastes the path to that file into the Terminal, which should now look like:
[bob-adamantium:~] bob% chmod 755 /Users/bob/Documents/utilityscripts/calc.pl
Press the Return key and if everything worked as it should you ought to see an empty command prompt appear under the line you just entered:
[bob-adamantium:~] bob% chmod 755 /Users/bob/Documents/utilityscripts/calc.pl
[bob-adamantium:~] bob%
A sure sign of success if ever there was one.
We're now at the point where you can use 'calc' like a calculator but not in a convenient way, and not by just typing calc. To use it now you'd have to type in the complete path to it so it can be found. Very inconvenient, that.
Instead we first need to alias this file to the calc command so the system has some idea of what we're trying to do when we type that. To accomplish this, type the following into the command prompt:
[bob-adamantium:~] bob% alias calc '/Users/bob/Documents/utilityscripts/calc.pl "\!*"'
[bob-adamantium:~] bob% alias calc='/Users/bob/Documents/utilityscripts/calc.pl'
That's very similar to creating an alias to an application in the Finder, basically a short-cut. If everything's gone according to plan and I haven't forgotten to tell you something you ought to be able to now type in the following:
[bob-adamantium:~] bob% calc 42*7
And see:
[bob-adamantium:~] bob% calc 42*7
294
[bob-adamantium:~] bob%
as a result.
Other fun things to try with calc:
[bob-adamantium:~] bob% calc pi
[bob-adamantium:~] bob% calc 'p=3; n=9; p*n+100'
[bob-adamantium:~] bob% calc -h
See more examples of 'calc' goodness here.
But wait! There's a problem: if we quit the Terminal then the very next time we launch it and type calc we'll get an error. The alias we just created isn't persistent, it dies when we quit. We need to make it persistent and for that we need to edit your .tcshrc file.
Fifth type the following at the command prompt:
[bob-adamantium:~] bob% locate .tcshrc
[bob-adamantium:~] bob% locate .bash_profile
'locate' is a sweet little utility that's similar to the Finder's Find command (Cmd-F) but much faster and it allows you search for files that the Finder won't, like system files and files that start with a period (ie: .tcshrc).
'locate' will return:
[bob-adamantium:~] bob% locate .tcshrc
/Users/bob/.tcshrc
[bob-adamantium:~] bob%
[bob-adamantium:~] bob% locate .bash_profile
/Users/bob/.bash_profile
[bob-adamantium:~] bob%
and that is the path to your .tcshrc .bash_profile file. If the file doesn't yet exist it's no big deal. You'll just have to create it anew in the next step.
Now type:
[bob-adamantium:~] bob% pico /Users/bob/.tcshrc
[bob-adamantium:~] bob% pico /Users/bob/.bash_profile
'pico' is a very simple text editor, like using TextEdit to edit a file except that none of the keyboard commands will be familiar, the interface is butt-ugly and its command-line driven. So why do we use it? Because we can, that's why.
'pico' will show you something like this:
UW PICO(tm) 2.5 File: /Users/bob/.tcshrc
UW PICO(tm) 2.5 File: /Users/bob/.bash_profile
in reversed text at the top of the screen. You may see other things already in this file, you may not. Regardless, on a new line we want to add this chunk of already-familiar text (it's our alias, from above):
alias calc '/Users/bob/Documents/utilityscripts/calc.pl "\!*"'
alias calc='/Users/bob/Documents/utilityscripts/calc.pl'
so we end up with:
UW PICO(tm) 2.5 File: /Users/bob/.tcshrc
alias calc '/Users/bob/Documents/utilityscripts/calc.pl "\!*"'
UW PICO(tm) 2.5 File: /Users/bob/.bash_profile
alias calc='/Users/bob/Documents/utilityscripts/calc.pl'
Remember I said that keyboard commands are going to seem strange in pico? To save the file press Control-O (that's an 'oh', not a zero). pico will return with:
File Name to write : /Users/bob/.tcshrc
File Name to write : /Users/bob/.bash_profile
Press the Return key and you'll then see:
[ Wrote 7 lines ]
(or '1 line' or '2 lines', however many lines there are in your .tcshrc file, mine happens to have seven). Now Control-X to quit pico and return back to the command prompt.
Sixth Stick a fork in it, it's done. That's it, that's all there is to getting a calculator up and running from the command line. To test it out and ensure that it's all working properly quit the Terminal, relaunch it, and type the following in at the command prompt:
[bob-adamantium:~] bob% calc pi* ( 67^2 )
If you see anything other than:
[bob-adamantium:~] bob% calc pi* ( 67^2 )
204.204
[bob-adamantium:~] bob%
then either you screwed up or I screwed up but somewhere something got screwed up. Sorry about that.
If you do see 204.204 (isn't that a nice circular area?) then congrats! Welcome to the wonderful world of command-line utility.
Cheers,
Chris Cummer (no, my name is not Bob)
2004-10-19 v1.0