Use Node.js as Shell

How might I set up node.js as a shell replacement for bash? For example I should be able to run vi('file') to open a file and cd('location') to change between directories.

Is this even possible?


Sure you can! It will become much less straightforward to use your computer, though.

First off, you will need to know how to set this up. While you could likely set your user shell in Linux to usr/bin/node, this will leave you with only a Node.js REPL with no additional programs set up. What you're going to want to do is write a setup script that can do all of the below setup/convenience steps for you, essentially something that ends with repl.start() to produce a REPL after setting everything up. Of course, since UNIX shell settings can't specify arguments, you will need to write a small C program that executes your shell with those arguments (essentially, exec("/usr/bin/node", "path/to/setup/script.js");) and set that as your UNIX shell.

The main idea here is that any commands that you use beyond the very basics must be require()d into your shell - e.g. to do anything with your filesystem, execute

var fs = require("fs")

and do all of your filesystem calls from the fs object. This is analogous to adding things to your PATH. You can get basic shell commands by using shelljs or similar, and to get at actual executable programs, use Node's built-in child_process.spawnSync for a foreground task or child_process.spawn for a background task.

Since part of your requirement is that you want to call each of your programs like a function, you will need to produce these functions yourself, getting something like:

function ls(path) {
    child_process.spawnSync('/bin/ls', [path], { stdio: 'inherit' });

for everything you want to run. You can probably do this programmatically by iterating through all the entries in your PATH and using something involving eval() or new Function() to generate execute functions for each, assigning them to the global object so that you don't have to enter any prefixes.

Again, it will become much less straightforward to use your computer, despite having these named functions. Lots of programs that cheat and use bash commands in the background will likely no longer work. But I can certainly see the appeal of being able to leverage JavaScript in your command-line environment.

I think it would be an intersting proposition. Create a test account and tell it to use node as it's shell. See 'man useradd' for all options

$ useradd -s /usr/bin/node test
$ su - test

This works on mac and linux.

require('child_process').spawnSync('vi', ['file.txt'], { stdio: 'inherit' })

Need Your Help

Programatically fetching Singleton instances that implement a common interface

c# .net reflection singleton

I'm working with some legacy ASP.NET MVC code that has a number of fairly detailed XML configuration files. Each file is loaded into an object representation defined as a Singleton class at runtime.

Flash Video Players: Do people really use the volume control?

flash video audio volume

Im wondering if anyone has any input on this subject? Im building a flash video player, and I have added a mute volume icon, but Im wondering what everyone's thoughts are on adding a volume control...