Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
The #! (hashbang or shebang) at the start of a file tells Unix systems which interpreter to use. JavaScript now officially supports this syntax, making executable .js files a standard feature!
#!/usr/bin/env node
console.log('Hello from executable JavaScript!');
console.log('Arguments:', process.argv.slice(2));chmod +x script.js
./script.js arg1 arg2#!/usr/bin/env node
// greet.js - A simple greeting tool
const name = process.argv[2] || 'World';
console.log(`Hello, ${name}!`);
// Usage: ./greet.js Alice
// Output: Hello, Alice!#!/usr/bin/env node
// process-file.js
import { readFileSync } from 'fs';
const filename = process.argv[2];
if (!filename) {
console.error('Usage: ./process-file.js <filename>');
process.exit(1);
}
try {
const content = readFileSync(filename, 'utf8');
console.log(`Lines: ${content.split('\n').length}`);
console.log(`Characters: ${content.length}`);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
// Usage: ./process-file.js data.txt#!/usr/bin/env node
// task.js - Simple task runner
const tasks = {
build: () => console.log('Building project...'),
test: () => console.log('Running tests...'),
deploy: () => console.log('Deploying...'),
};
const taskName = process.argv[2];
if (!taskName || !tasks[taskName]) {
console.log('Available tasks:', Object.keys(tasks).join(', '));
process.exit(1);
}
tasks[taskName]();
// Usage: ./task.js buildThe #! must be the very first thing in the file - no empty lines or comments before it!
Hashbang is for Unix/Linux/Mac. Windows doesn't use it (but Node.js still parses it correctly)
Run chmod +x file.js to make the file executable
#!/usr/bin/env node at file start
Makes JS files runnable directly
Perfect for command-line utilities
Official JavaScript standard