Skip to content

Permit combining stdout/stderr as streams to external command behind pipe #9673

Closed

Description

@x10an14

Question

Given command foo, which outputs both stdout and stderr, how can I pipe both of stdout and stderr from foo into bar through a pipe?

Ref these (non-exhaustive list of) alternatives which achieve this in bash:

foo |& bar
foo 2>&1 | bar

Additional context and details

The only way for me to automate checking of my flake.nix checks (in a nice graphical output) in Nushell when using nom now is this arcane invocation:

bash -c 'nix flake check --log-format internal-json -vv |& nom --json'

Feels weird that I cannot do the equivalent of "combine outputs and redirect them through pipe" in Nushell =S

Activity

  1. added
    deprecated:question
    on Jul 13, 2023
  2. fdncred commented on Jul 13, 2023

    @fdncred
    Contributor

    You could try the complete command but it won't interleave stderr and stdout.

    ❯ do { ^ls } | complete
    ╭───────────┬─────────────────────╮
    │ stdout    │ CODE_OF_CONDUCT.md  │
    │           │ CONTRIBUTING.md     │
    │           │ Cargo.lock          │
    │           │ Cargo.toml          │
    │           │ Cross.toml          │
    │ stderr    │                     │
    │ exit_code │ 0                   │
    ╰───────────┴─────────────────────╯
    
  3. hardfau1t commented on Jul 14, 2023

    @hardfau1t
    Contributor

    You could try the complete command but it won't interleave stderr and stdout.

    • it won't work for stream data, data will be availabe once the program exits
  4. changed the title [-]How can I redirect stdout and stderr (combined) as stdin through a pipe?[/-] [+]How can I redirect stdout and stderr (combined as a stream) as stdin through a pipe?[/+] on Jul 14, 2023
  5. x10an14 commented on Jul 14, 2023

    @x10an14
    Author

    You could try the complete command but it won't interleave stderr and stdout.

    • it won't work for stream data, data will be availabe once the program exits

    Thank you for the clarification! The intent behind the example given is to receive streamed data to get a nice visualization of the progress =)

  6. x10an14 commented on Jul 14, 2023

    @x10an14
    Author

    Considering the replies received so far, should this become a feature request instead?

  7. hardfau1t commented on Jul 14, 2023

    @hardfau1t
    Contributor

    Considering the replies received so far, should this become a feature request instead?

    IMO it should be, AFAIK there is no straight way to do this(not sure whether it is possible or not) and it is most common in unix systems

  8. amtoine commented on Jul 14, 2023

    @amtoine
    Member

    @x10an14
    you can just update the title and the body of this issue to make it a feature request 😉

  9. changed the title [-]How can I redirect stdout and stderr (combined as a stream) as stdin through a pipe?[/-] [+]Permit combining stdout/stderr as streams to external command behind pipe[/+] on Jul 14, 2023
  10. x10an14 commented on Jul 14, 2023

    @x10an14
    Author

    @amtoine: @x10an14 you can just update the title and the body of this issue to make it a feature request 😉

    Will you replace the question label/add the feature flag label? =)

  11. added
    category:enhancement
    and removed
    deprecated:question
    on Jul 15, 2023
  12. fdncred commented on Jul 15, 2023

    @fdncred
    Contributor

    done

  13. amtoine commented on Jul 15, 2023

    @amtoine
    Member

    thanks @x10an14 😊

  14. added
    A:streaming
    A:redirection-pipe
    on Jul 18, 2023
  15. haitemura commented on Aug 14, 2023

    @haitemura

    I had the same problem just now and went with run-external --redirect-combine foo | bar. But a shorter way to do this would help.

  16. x10an14 commented on Aug 14, 2023

    @x10an14
    Author

    I had the same problem just now and went with run-external --redirect-combine foo | bar. But a shorter way to do this would help.

    Can you link/reference/document the redirect-combine flag? And never heard of it or it's command. I have yet to check if it's a built-in

  17. fdncred commented on Aug 14, 2023

    @fdncred
    Contributor
  18. x10an14 commented on Aug 16, 2023

    @x10an14
    Author

    The run-external --redirect-combine tip works! But only if I quote each and every word starting with a hyphen (read: flag) like so (it seems):

    run-external --redirect-combine nix flake check "--log-format" internal-json "-v" | nom --json

    While this is of course much better than my previous "use bash" solution, would run-external (by its intent and nature) not be better suited to pass all inputs starting with - to the command its encompassing/running?

    Dunno how, but force ordering of flags for run-external to come before the wrapped command maybe?

  19. x10an14 commented on Aug 16, 2023

    @x10an14
    Author

    This is how it looks like if I don't quoute all flags sent to wrapped binary/command:

    2023-08-16 17:18:03+02:00: ~/Doc/sr./nix-configs via ❄️  impure (nix-shell-env)
    x10an14@nav-840G8-laptop ❯ > run-external --redirect-combine nix flake check "--log-format" internal-json -v | nom --json
    Error: nu::parser::unknown_flag
    
      × The `run-external` command doesn't have flag `-v`.
       ╭─[entry #3:1:1]
     1 │ run-external --redirect-combine nix flake check "--log-format" internal-json -v | nom --json
       ·                                                                               ┬
       ·                                                                               ╰── unknown flag
       ╰────
      help: Available flags: --help(-h), --redirect-stdout, --redirect-stderr, --redirect-combine, --trim-end-newline.
            Use `--help` for more information.
    
    
  20. CAD97 commented on Oct 23, 2023

    @CAD97
    Contributor

    A helper to prevent run-external from trying to interpret your command's flags:

    def --wrapped run [cmd ...args] { run-external --redirect-combine $cmd $args }

    For the convenience you give up the ability to control the flags given to run-external, but get an experience much closer to how ^ works. Each flag you define on run can't be passed through without quoting, but using the wrapper limits that to exclusively just the automatic --help(-h) switch.

    A more principled solution would be to teach Nu how to use -- to delineate the end of flags and that all following args are rest arguments. The best solution would probably be to be able to combine "retroactively" and do something like ^external | combine like you can do ^external | complete.

  21. added a commit that references this issue on Feb 8, 2024
    58c6fea
  22. added this to the v0.91.0 milestone on Feb 9, 2024
  23. added a commit that references this issue on Feb 20, 2024
    b960699
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:redirection-pipeA:streamingcategory:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions