Search code examples
shellkshtee

How can I redirect the same message to both stdoud and stderr (without temporary file)


It is easy to redirect stdout or stderr to the same output (file or one of the 2 std output) with a 1>&1,>&2 and/or >file but is there a way to send the same output to both the std output in KSH (like | tee File but with &2 as file) ?


Solution

  • #!/bin/ksh
    
    function mytee { 
            while read -r x ; do
                    print "${x}"
                    print "${x}" >&2
            done
    }
    
    echo "TestMyTee
    second line
    and a * character" | mytee
    
    # Really stdout and stderr?
    echo "TestMyTee
    second line
    and a * character" | mytee 1>mystdout 2>mystderr
    

    Edited: replaced [read] and [echo] by [read -r] and [print]