Search code examples
node.jsperlshell-execbackticks

What's the equivalent of Perl's backticks in Node.js?


In Perl you can do something like

perl -E'my $date = `date`; say $date'

This is a terse way to run a command synchronously and set the variable to STDOUT. In Node, how can I quickly run a command to capture the stdout?


Solution

  • Try using .execSync with an explicit encoding.. In the shortest form (with whitespace),

    var date = require('child_process').execSync('date', {encoding: 'utf8'} )
    console.log(date);