Is there a JavaScript shell?

24,979

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

Solution 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

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

Share:
24,979

Related videos on Youtube

J-unior
Author by

J-unior

Updated on September 18, 2022

Comments

  • J-unior
    J-unior almost 2 years

    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?

  • J-unior
    J-unior over 11 years
    I think you didn't understand the question. I've asked about UNIX shell. But thanks for the willing to help anyway.
  • J-unior
    J-unior over 11 years
    Well, that looks cool! I'll give it a try.
  • William
    William over 8 years
    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 gnp.github.io/psh There might be a counter example but I haven't find it yet.
  • William
    William over 8 years
    How would I set this as my login shell? Again I want something like this but I'm just not convinced this is it.
  • goldilocks
    goldilocks over 7 years
    This is available on some/many linux distros now as a js package.
  • inf3rno
    inf3rno almost 7 years
    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.
  • roaima
    roaima over 6 years
    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.)
  • yO_
    yO_ over 5 years
    ShellJS link is broken.