34

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?

Renan
  • 16,976
  • 8
  • 69
  • 88
J-unior
  • 691
  • 3
  • 7
  • 15

4 Answers4

19

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).

sr_
  • 15,224
  • 49
  • 55
  • Well, that looks cool! I'll give it a try. – J-unior Dec 09 '12 at 16:32
  • I technically don't really consider this a shell because it doesn't follow the `cmd arguments` syntax. For example `psh` adds such syntax to the perl language from my understanding so it appears more like a typical shell http://gnp.github.io/psh There might be a counter example but I haven't find it yet. – William Oct 20 '15 at 04:21
  • How would I set this as my login shell? Again I want something like this but I'm just not convinced this is it. – William Nov 22 '15 at 03:12
  • Still bash-ish, we could use `sed({i:1}, 'BUILD_VERSION', 'v0.1.2', file)` instead. Would be better to use async functions with await, so parallel would be faster. – inf3rno Sep 13 '17 at 11:40
  • ShellJS link is broken. – yO_ Oct 30 '18 at 14:26
10

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

arielf
  • 840
  • 9
  • 14
9

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.

Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
0

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.

  • 7
    I think you didn't understand the question. I've asked about **UNIX** shell. But thanks for the willing to help anyway. – J-unior Dec 09 '12 at 14:54
  • 1
    This answers the question, "_is there a shell that uses JS as a scripting language?_". Nothing in the question talks about an alternative to `bash` or `zsh` (etc.) – roaima Feb 05 '18 at 21:44