What is exec-sh?
The exec-sh npm package is a simple utility for executing shell commands in Node.js. It provides a straightforward API to run shell commands synchronously or asynchronously, with options to handle output and errors.
What are exec-sh's main functionalities?
Execute a command asynchronously
This feature allows you to execute a shell command asynchronously. The callback function handles any errors and provides the exit code if the command fails.
const execSh = require('exec-sh');
execSh('echo Hello World', function(err){
if (err) {
console.log('Exit code:', err.code);
return;
}
console.log('Command executed successfully');
});
Execute a command synchronously
This feature allows you to execute a shell command synchronously. If the command fails, an error is thrown, and you can catch it to handle the exit code.
const execSh = require('exec-sh');
try {
execSh.sync('echo Hello World');
console.log('Command executed successfully');
} catch (err) {
console.log('Exit code:', err.code);
}
Capture command output
This feature allows you to capture the standard output and standard error of the executed command. The callback function provides the stdout and stderr as arguments.
const execSh = require('exec-sh');
execSh('echo Hello World', true, function(err, stdout, stderr){
if (err) {
console.log('Exit code:', err.code);
return;
}
console.log('stdout:', stdout);
console.log('stderr:', stderr);
});
Other packages similar to exec-sh
child_process
The child_process module is a built-in Node.js module that provides the ability to spawn child processes. It offers more control and flexibility compared to exec-sh, including options to spawn, fork, exec, and execFile processes.
shelljs
ShellJS is a portable (Windows/Linux/macOS) implementation of Unix shell commands on top of Node.js. It provides a more extensive set of shell commands and utilities compared to exec-sh, making it suitable for more complex scripting tasks.
execa
Execa is a modern alternative to child_process. It provides a simpler and more powerful API for executing shell commands, with better support for promises and improved error handling. It is more feature-rich compared to exec-sh.
exec-sh
Execute shell command forwarding all stdio streams.
Features
exec-sh is a wrapper for child_process.spawn
with some improvements:
- Cross platform command execution:
- Windows:
cmd /C COMMAND
- others:
sh -c COMMAND
- Fowrards all stdio streams to current terminal (by default):
execSh("bash")
execsh("echo -n Say: && read i && echo Said:$i")
- stdout and stderr are passed to callback when available
execSh("pwd", console.log)
Showcase
execSh("echo hello exec-sh && bash", { cwd: "/home" }, function(err){
if (err) {
console.log("Exit code: ", err.code);
}
});
hello exec-sh
bash-3.2$ pwd
/home
bash-3.2$ exit 99
exit
Exit code: 99
Usage
var execSh = require("../");
execSh("echo lorem && bash", { cwd: "/home" }, function(err){
if (err) {
console.log("Exit code: ", err.code);
return;
}
var child = execSh(["bash -c id", "echo lorem >&2"], true,
function(err, stdout, stderr){
console.log("error: ", err);
console.log("stdout: ", stdout);
console.log("stderr: ", stderr);
});
});
Promise Interface
var execShPromise = require("exec-sh").promise;
const run = async () => {
let out;
try {
out = await execShPromise('pwd', true);
} catch (e) {
console.log('Error: ', e);
console.log('Stderr: ', e.stderr);
console.log('Stdout: ', e.stdout);
return e;
}
console.log('out: ', out.stdout, out.stderr);
}
run();
Public API
execSh(command, [options], [callback])
Execute shell command forwarding all stdio.
Parameters:
command {String|Array}
- The command to run, or array of commands[options] {Object|TRUE}
- Options object passed directly to child_process.spawn
, when TRUE
then { stdio: null }
used[callback] {Function}
- callback(err, stdout, stderr)
err {Error|NULL}
- Error object. Has code
property containing last command exit code when availablestdout {String|NULL}
- aggregated stdout or NULL
if not availablestderr {String|NULL}
- aggregated stderr or NULL
if not available
Return Values:
Returns ChildProcess object.
Private API
Complete API Documentation including private and public methods is generated from source code by JSDoc tool and is available here.
Code Coverage
Code coverage report for all files is available here.
Scripts
npm test
- run testsnpm run jsdoc
- build jsdocnpm run dev
- run tests continuously
License
The MIT License (MIT)