1

I can escape the cli arguments node consumers with -- and have hi be my arg that's used in the text node evaluates, but I can't do the same piping a value.

j at MBP in ~
$ node -e 'console.log(">", process.argv[1])' -- hi
> hi

The piping here in bash should just take stdout and add it as an arg for the command it gets piped right?

j at MBP in ~
$ echo hi | node -e 'console.log(">", process.argv[1])' --
> undefined
James T.
  • 812
  • 10
  • 24

1 Answers1

3

The pipe | feeds the output from the echo command into stdin on the node command. You'll need to use the readline module to read in a fluid way from stdin -- there are other ways too, but this is easy enough. (stolen from here).

var readline = require('readline');
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

rl.on('line', function(line){
    console.log(line);
})

If you want to use the output from echo as a command line parameter, you need to use backticks:

node -e 'console.log(">", process.argv[1])' -- `echo hi`

or a subshell:

node -e 'console.log(">", process.argv[1])' -- $(echo hi)
PaulProgrammer
  • 14,221
  • 3
  • 35
  • 54