
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
bash-emulator
Advanced tools
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.
The module can be used in Node.js or in the browser. Get it with:
npm install --save bash-emulator
The 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
state an optional object to initialize the state. For shape see below.emulator objectemulatorrun(command) -> Promise(output, code)
command a bash command as stringPromise that resolves with an output string.getDir() -> Promise(path)
changeDir(path) -> Promise
path relative path to set working directory toread(filePath) -> Promise(content)
filePath relative path of file to readreadDir(path) -> Promise([files])
path optional, relative path of directory to read. Defaults to current directory.stat(path) -> Promise(stats)
path optional path of file or directory. Defaults to current directory.modified.createDir(path) -> Promise
path relative, non-existed path for new directorywrite(filePath, content) -> Promise
filePath path of file that should be written to. File doesn't have to exist.remove(path) -> Promise
path path of file or directory to deletecopy(source, destination) -> Promise
source path of file or directory to copydestination target path. Will be overwritten if existent.getHistory() -> Promise([commands])
completeUp(input) -> Promise(command)
str command that should be completedundefined if no completion foundcompleteDown(input) -> Promise(command)
completeUpstr command that should be completedundefined if no completion foundcommands
state
ls -l -acdpwdhistorycat -ncleartouchmkdirmv -ncp -r -Rrm -r -Rrmdirstate objectIt's not recommended to access the state directly. Use the above defined helper methods instead.
history an array of strings containing previous commandsuser name of the current user (defaults to "user")workingDirectory a string containing the current working directory (defaults to /home/user)fileSystem an object that maps from absolute paths to directories or files.
type property thats either 'dir' or 'file'
and a modified property containing a unix timestampcontent property./home/userlocalStoragevar 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) {}
env object with:
output(string) call to write a string to stdouterror(string) call to write a string to stderrexit(code) call to exit command.
code integer to mark state of exit. Failure when not 0 (optional)system reference to the emulator object.args array from command string. First element is command name.{ 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.
npm installnpm testbash-emulator.min.js file with npm run buildWe 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
FAQs
Emulate a bash environment in plain js. Can be used in the browser.
We found that bash-emulator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.