Is there a JavaScript shell?

Recently I’ve been working with JS and I’m very enthusiastic about this language. I know that there is node.js for running JS at server side, but is there a shell that uses JS as a scripting language? If such thing exists, how usable & stable is it?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Does this look desirable to you?

// Replace macros in each .js file
cd('lib');
ls('*.js').forEach(function(file) {
  sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
  sed('-i', /.*REMOVE_THIS_LINE.*n/, '', file);
  sed('-i', /.*REPLACE_LINE_WITH_MACRO.*n/, cat('macro.js'), file);
});
cd('..');

If so, ShellJS could be interesting, it’s

a portable (Windows included) implementation of Unix shell commands on top of the Node.js API.

I’m unsure if this could be used as a full-featured login shell, though. (Maybe with some wrapping?)

You could argue that it’s not really a shell, but do you know TermKit? It’s made of Node.js + Webkit, you could use JS to extend it (I guess); the shell language is still Bash(-ish).

Method 2

If you’re on Ubuntu or any other debian based system, you may install rhino (from Mozilla.org).

sudo apt-get install rhino

It supplies js via alternatives:

=== /usr/bin/js is a symlink... following it
lrwxrwxrwx 1 root root 20 Nov 21 08:54 /usr/bin/js -> /etc/alternatives/js
=== /etc/alternatives/js is a symlink... following it
lrwxrwxrwx 1 root root 14 Nov 21 08:54 /etc/alternatives/js -> /usr/bin/rhino
=== /usr/bin/rhino is owned by package: rhino ===

So calling either rhino or js will give you a JavaScript shell.

Edit (2014-06-30):

rhino is good to quickly test some javascript code in a file, but it is not an interactive shell, so it doesn’t support GNU readline style of editing. For interactive work, you may prefer nodejs: on Ubuntu/debian sudo apt-get install nodejs. This should provide a more interactive js shell (invoke using the command js) where you can edit lines and recall history with the up/down arrow-keys. For a longer list of options, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells

Method 3

Mozilla INC offers a javascript shell : see Introduction_to_the_JavaScript_shell

Example of a shell in a system :

$ js
js> str = "welcome on *nix side" 
"welcome on *nix side"
js> str.indexOf("nix");          
12
js>

No problems detected since I use it for testing purpose.

Method 4

In Chrome and Firefox, if you press F12 it brings up developer tools. Over there you could find a tab called console, where you could type and test JavaScript scripts. It has code completion and other features too.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x