Home >Web Front-end >JS Tutorial >How to Execute Shell Commands Using JavaScript?
Executing Shell Commands with JavaScript
Executing system shell commands within JavaScript allows you to interact with the operating system from within your JavaScript code.
Approach:
To execute shell commands in JavaScript, you can utilize Node.js's child_process module. This module provides the exec function, which allows you to run a specified command and capture its output.
Code Example:
const { exec } = require('child_process'); exec('ls *.js bad_file | wc -l', (error, stdout, stderr) => { console.log('stdout:', stdout); console.log('stderr:', stderr); if (error) { console.error('exec error:', error); } });
Explanation:
The above is the detailed content of How to Execute Shell Commands Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!