I have a bash script that calls a function. The function, amongst other things, executes a pipeline that sinks its output. To simplify it, here is a contrived example:
#!/bin/bash
func() {
ls "$@" | sort | rev > /tmp/output
}
func "$@"
You would then do something like this to run it. It would do its stuff and deliver its payload to a file. There is no output to the screen.
$ ./myscript .
Now, say I want the output of sort
on standard output. How would I do that?
I can achieve it like this:
ls "$@" | sort | tee /dev/tty | rev > /tmp/output
However, using /dev/tty
is wrong, because this won't work:
$ ./myscript > myfile
Is there a more correct way to refer to the standard output of a bash script from within a pipleine inside a function ?
(I am using bash 4.3.0 on Arch Linux)
ls "$@" | sort | rev
. – terdon Oct 16 '14 at 14:01tee /dev/fd/[num]
totee
your output to anywhere.echo 'out twice' | { { tee /dev/fd/2 | grep . >&3 ; } 2>&1 | grep . ; } 3>&1 | nl
– mikeserv Oct 16 '14 at 16:30tee
and creating a separate file descriptor. The other question is asking the same thing also, how to get the output of a part of a pipeline. I don't see the difference you refer to but that may just be my own ignorance. If you still feel that it's not a dupe, please post a question on [meta] where it can be discussed. – terdon Oct 17 '14 at 14:59