Thursday, September 3rd, 2009

JavaScript on the command line or as a shell scripting language

Category: Java, JavaScript

I am always fascinated that no matter how shiny the OS, sooner or later you’ll end up typing things into a text console to get the darn machine to do what you want it to. To make matters worse you also have to resort to languages that you don’t use every day but are from cryptic days of development where keyboards obviously had more interesting characters like ~ | — and others easily available and scripts had to have numerous | in any line of code.

Enter Stoyan Stefanov who wrote a neat little blog post explaining how you can use JavaScript as a shell language.

In essence, Rhino is the answer for *nix machines but if you cuddle the penguin you most likely are a shell master anyways.

If your world features Gates and Windows, then there is the Windows Scripting Host, which has a tool to execute JavaScript on the DOS shell:


  1. C:\> cscript jslint.js

Macs have a tool called JSC which is “conveniently” located in:


  1. /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc

To make it human usable, creating a shortcut is a good plan:


  1. $ sudo ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /bin/jsc

You can then execute JavaScript by starting JSC which gives you something like the Firebug JS console in the OS console (without the console is not defined messages *in* the console).

With a few more lines using the print(), quit() and load() commands available on the shell you can then use any JavaScript to mess with your files and folders.

Posted by Chris Heilmann at 7:25 am

4 rating from 27 votes


Comments feed TrackBack URI

though the markup and is there… the two longer pre tags you used appear truncated in this post


$ sudo ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /bin/jsc

Comment by naterkane — September 3, 2009

I use Rhino on the shell all the time – when I want an interactive JavaScript interpreter. But this is a far cry from having the whole ecosystem of Unix shell commands available to you for your pipelining pleasure. If you want to get around on your system, you need to learn a shell!

Comment by sentientholon — September 3, 2009

Any pointer on messing with files and folders ? Some FileUtils equivalent?

Comment by jpjoyal — September 3, 2009

Anybody here saavy to why this does not work as expected?

create file foo:

print(‘hello world’);

chmod 755 foo


no output… ajaxian?!

Comment by zachstronaut — September 3, 2009

FWIW, I think there’s also a readline() built-in for grabbing data from STDIN. So, no native file access, but you can pipe things in. That’s useful at least for using things like JSLint

Comment by lmorchard — September 3, 2009

(and by readline(), I mean readline() in jsc)

Comment by lmorchard — September 3, 2009

IMHO, if you need a command-line scripting language, install Python. It has cleaner syntax than Javascript, but has so many similarites that a JS programmer could be comfortable with it in an afternoon.

Comment by tercero12 — September 3, 2009

By default, WSH (Windows Script Host) only executes JavaScript from a file. I’ve written some JavaScript that makes WSH act like an interactive interpreter (read-eval-print loop). There’s a page describing it at: The really cool thing about running JavaScript this way is that you’re not confined by the Same Origin Policy, so you can script across web sites. Think of it as a shell for the Web, rather than a shell for your local box.

Comment by TylerClose — September 3, 2009


I tried doing that too, is probably because jsc is trying to execute the script including the first line with the shebang and stops because it’s not valid js, I haven’t found an alternative, but it would be really cool if we could have js shell scripts.

Comment by reaktivo — September 3, 2009

If you’re looking for a scripting language with simplicity and power look at rebol. It’s closer to Javascript than you may think see

For example you will be able to tweet in one line in command line:

Here’s tips for improving productivity with command line

Comment by rebtut — September 4, 2009

Narwhal aims to be a complete environment for JavaScript on the server, including a standard library (implementing the CommonJS/ServerJS spec), package manager, shell, virtual environments, etc.

It’s a layer on top of the interpreters, so you’ll be able to switch between Rhino, V8, JSC, etc. For now Rhino is the most complete.

It’s still a work in progress, but useful none the less:

Comment by tlrobinson — September 5, 2009

JavaScript OSA is a port of the Mozilla JavaScript scripting system to the Macintosh:

Comment by randomrandom — September 5, 2009

This is very interesting as well —

JSTalk is how I imagine AppleScript would be done today, if it was written by a Cocoa programmer who was smitten by Cocoa and Objective-C…

Comment by randomrandom — September 5, 2009

Leave a comment

You must be logged in to post a comment.