zx: $verbose = false is not displaying command output. verbose should be just for the command

Works for me.

⌘ zx ⎇ main ❯❯❯ cat foo.mjs
#!/usr/local/bin/node
import { $ } from './index.mjs'
$.verbose = false
await ($`uptime`)
⌘ zx ⎇ main ❯❯❯ node foo.mjs
⌘ zx ⎇ main ❯❯❯

_Originally posted by @antonmedv in https://github.com/google/zx/issues/80#issuecomment-841612690_

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 36

Most upvoted comments

@adrianord for this specific case you need to do something like this:

$.verbose = false
await $`program 2>&1`.pipe(process.stdout)

Or if you want to redirect in to stderr:

  $.verbose = false
  let p = $`program`
  p.stdout.pipe(process.stdout)
  p.stderr.pipe(process.stderr)
  await p

Got something even more interesting:

await $`npm init`.pipe(process.stdout)

I would be fine doing something like

$.verbose = false
$.output = true
await $`date`
$.output = false
let foo = await $`pwd`

personally. I think it’s unrealistic to for zx to be one to one with bash, but I’d like the option to not display the command that is being ran.

Please forgive me this is off-topic, but this is the only place I can post this.

I’ve been looking at proxy-www which I found on Modern Javascript: Everything you missed over the last 10 years (2020) via a post on hn wondering if it would be possible to simplify the syntax you demonstrated to something like this:

let {stdout} = await $.pipes
.echo(`"hello"`)
.awk(`'{print $1" world"}'`)
.tr(`'[a-z]' '[A-Z]'`)

or even something like this, maybe (using Korn shell if available):

let {stdout} = await $.bin.korn
.echo(`-ne "hello\n"`) // -ne is just an example arg to show that everything is still pretty much the same.
.awk(`'{print $1" world"}'`)
.program(`2>&1`)
.bork(`2>/dev/null`)
.ls('-lh', '/usr') // <-- look here it is a raw arg array for node's child_process.spawn you grab it with ...arg
.tr(`'[a-z]' '[A-Z]'`)

Again, apologies for the off-topic post. Your program is great, love the take on Knuth’s Literate Programming where you allow zx examples/index.md

@antonmedv commands that output stdout to show progress will only show their full output at the end of execution with the work around you are proposing, this is not desirable. With using verbose they output to stdout as they are running.

@antonmedv I’m OK with the pipeline method! Thanks!

This code already in the master branch and can be tested!