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.
Showcase
// JavaScript
execSh("echo hello exec-sh && bash", { cwd: "/home" }, function(err){
if (err) {
console.log("Exit code: ", err.code);
}
});
// Terminal output: interactive bash session
hello exec-sh
bash-3.2$ pwd
/home
bash-3.2$ exit 99
exit
Exit code: 99
Features
exec-sh is a wrapper for child_process.spawn with improvements:
- Cross platform command execution:
- Windows:
cmd /C COMMAND
- others:
sh -c COMMAND
- Fowrards all stdio streams to current terminal (by default):
- try
execSh("bash")
- try
execsh("echo -n Say: && read i && echo Said:$i")
- stdout and stderr are passed to callback when available
- try
execSh("pwd", console.log)
Installation
npm install exec-sh
Usage
var execSh = require("../");
// run interactive bash shell
execSh("echo lorem && bash", { cwd: "/home" }, function(err){
if (err) {
console.log("Exit code: ", err.code);
return;
}
// collect streams output
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);
});
});
Release history
Testing
npm test
License
The MIT License (MIT)