Skip to content

Understanding multiple redirection #2463

@kofalt

Description

@kofalt

Suppose I wanted to manipulate multiple output streams of a command, without changing or combining what output is sent to which stream. This is ideal for modularity, so that a convenience filter (or whatever) doesn't preclude other, later uses.

Creating an example stuff script that writes to stdout and stderr:

#!/bin/bash

echo "One"
echo "Two" 1>&2

I could fiddle with stdout via a pipe:

./stuff | sed 's/One/1/g'

# Stdout: 1
# Stderr: Two

To simultaneously fiddle with stderr, I wrap everything in a block and use 2>|:

begin; ./stuff | sed 's/One/1/g'; end 2>| sed 's/Two/2/g' 1>&2

# Stdout: 1
# Stderr: 2

Success! Both streams were processed, without losing out/err information.

I think the closest analogue in Bash is the following:

./stuff 1> >(sed 's/One/1/g') 2> >(sed 's/Two/2/g' 1>&2)

# Stdout: 1
# Stderr: 2

This is bit more terse, but inferior to the fish example as the shell returns early.


I have two questions:

  1. Is my fish example correct & reasonable?
  2. Is there a better way to do multiple redirection in fish that I should be aware of?

Thanks! 😎

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions