This module can help you if you like to emulate a bash shell in plain Javascript.
It might be useful for education purposes or even to ease the interaction with a back-end system.
bash-emulator implements an environment for bash commands to run in and it also includes some default commands.
The system it provides can be thought of like the syscalls of an operating system.
Shell commands are programs running on top of this low-level primitives.
The system doesn't handle any UI interaction but provides hooks to communicate with other systems. It's easy to add new commands and customize the underlying file system.
Also note that even though we try to create a realistic bash environment, this system won't behave identical to your local environment. Before using commands for other projects you should also try them with a real shell.
If you are looking for a real terminal to run in your browser, have a look at xterm.js.
The module can be used in Node.js or in the browser. Get it with:
npm install --save bash-emulatorThe module exports one function that can be required from another module. Please use a tool like webpack or browserify for bundling and minification in your own workflow.
bashEmulator(state) -> emulator
- statean optional object to initialize the state. For shape see below.
- Returns an emulatorobject
- run(command) -> Promise(output, code)- commanda bash command as string
- Returns a Promisethat resolves with an output string.
 
- getDir() -> Promise(path)- Returns a Promise that resolves with the current working directory
 
- changeDir(path) -> Promise- pathrelative path to set working directory to
- Returns a Promise that resolves when change is done
 
- read(filePath) -> Promise(content)- filePathrelative path of file to read
- Returns a Promise that resolves with the content of the file
 
- readDir(path) -> Promise([files])- pathoptional, relative path of directory to read. Defaults to current directory.
- Returns a Promise that resolves with an array of file names.
 
- stat(path) -> Promise(stats)- pathoptional path of file or directory. Defaults to current directory.
- Returns a Promise that resolves with a stats object. For now, only property is modified.
 
- createDir(path) -> Promise- pathrelative, non-existed path for new directory
- Returns a Promise that resolves when directory is created
 
- write(filePath, content) -> Promise- If file isn't empty, content is appended to it.
- filePathpath of file that should be written to. File doesn't have to exist.
- Returns a Promise that resolves when writing is done
 
- remove(path) -> Promise- pathpath of file or directory to delete
- Returns a Promise that resolves when deleting is done
 
- copy(source, destination) -> Promise- sourcepath of file or directory to copy
- destinationtarget path. Will be overwritten if existent.
- Returns a Promise that resolves when copying is done
 
- getHistory() -> Promise([commands])- Returns a Promise that resolves with a array containing all commands from the past
 
- completeUp(input) -> Promise(command)- Complete a command from history
- Can be called multiple times to go further back in history
- See example for connecting arrow-keys with completion
- strcommand that should be completed
- Returns a Promise with a command, is undefinedif no completion found
 
- completeDown(input) -> Promise(command)- Move in opposite direction to completeUp
- strcommand that should be completed
- Returns a Promise with a command, is undefinedif no completion found
 
- Move in opposite direction to 
- commands- An object with all commands that the emulator knows of
 
- state
- pipes |
- ls -l -a
- cd
- pwd
- history
- cat -n
- touch
- mkdir
- mv -n
- cp -r -R
- rm -r -R
- rmdir
It's not recommended to access the state directly. Use the above defined helper methods instead.
- historyan array of strings containing previous commands
- username of the current user (defaults to- "user")
- workingDirectorya string containing the current working directory (defaults to- /home/user)
- fileSysteman object that maps from absolute paths to directories or files.- Each value has a typeproperty thats either'dir'or'file'and amodifiedproperty containing a unix timestamp
- Files also have a contentproperty.
- Default file system contains only directories for /home/user
 
- Each value has a 
var state = JSON.parse(localStorage.bashEmulator || '{}')
var emulator = bashEmulator(state)
function saveState () {
  localStorage.bashEmulator = JSON.stringify(emulator.state)
}
emulator.run().then(saveState)You can modify the commands object of your emulator instance
to your liking.
To add a new command you need to implement the following API:
var emulator = bashEmulator()
emulator.commands.myCommand = function (env, args) {}- envobject with:- output(string)call to write a string to stdout
- error(string)call to write a string to stderr
- exit(code)call to exit command.- codeinteger to mark state of exit. Failure when not- 0(optional)
 
- systemreference to the emulator object.
 
- argsarray from command string. First element is command name.
- Optionally return object to register handlers for events:
{ input: fn, close: fn }
You can ignore the simple, built-in file system and overwrite all required methods of your emulator instance with custom implementations. The API of the methods are designed to work with asynchronous implementations as well.
- Make sure you have node.js installed
- Setup project using npm install
- Make sure tests are passing using npm test
- Build the bash-emulator.min.jsfile withnpm run build
We are happy to accept new contributions!
It can be a fun experience to re-implement some programs you already know.
This can give you some new insights in how they work.
You can also try out strace to find out how commands work on your local system!
Just make sure the tests are passing (npm test) and send a Pull Request.
If you are looking for a new feature to implement, make sure to have a look at our roadmap.
To support IE, please use a promise polyfill. For example: https://github.com/stefanpenner/es6-promise