Search code examples
bashshellstderr

Redirect stderr to console and file


How can i redirect sdterr of bash script to console and file? I am using:

exec 2>> myfile

to log It to myfile. How to extend it to log to console as well?


Solution

  • You can create a fifo

    $ mknod mypipe p
    

    let tee read from the fifo. It writes to stdout and the file you specified

    $ tee myfile <mypipe &
    [1] 17121
    

    now run the command and pipe stderr to the fifo

    $ ls kkk 2>mypipe 
    ls: cannot access kkk: No such file or directory
    [1]+  Done                    tee myfile < mypipe