#] #] ********************* #] "$d_SysMaint"'Linux/output redirection notes.txt' - output redirection ideas # www.BillHowell.ca 25Oct2021 initial To view this file - use a text editor (not word processor) constant width font (eg courrier 10), tab - 3 spaces #24************************24 /media/bill/Dell2/Website - raw/System_maintenance/Linux/output redirection notes.txt #24************************24 # Table of Contents, generated with : # $ grep "^#]" "$d_SysMaint"'Linux/output redirection notes.txt' | sed 's/^#\]/ /' # ********************* #08********08 #] 25Oct2021 example str_overWriteTo_pthOrHandle IS OP str pthOrHandle Note that you don't actually put in the stdout fileHandle # str_overWriteTo_pthOrHandle IS OP str pthOrHandle - res ipsa loquitor # 25Oct2021 initial str_overWriteTo_pthOrHandle IS OP str pthOrHandle { LOCAL ; IF (isinteger pthOrHandle) THEN host link 'echo ' chr_apo str chr_apo ; ELSE host link 'echo ' chr_apo str chr_apo ' >"' pthOrHandle '" ' ; ENDIF ; } # str_overWriteTo_pthOrHandle 'This is a test.' # str_overWriteTo_pthOrHandle 'This is a test.' (link d_temp 'str_overWriteTo_pthOrHandle test.txt') #08********08 #] 25Oct2021 - basics, also copied to 'tee notes.txt' as this good example for that, too #] normal output if not otherwise specified is stdout (terminal) (implicit '>1') #] >"" 2>&1 - redirect stderr to , note that the redirection occurs AFTER specifying the path +-----+ search 'Linux file handle for stdout' https://unix.stackexchange.com/questions/333198/bash-redirect-stderr-to-file-and-stdout-stderr-to-screen +--+ With a recent bash, you can use process substitution. foo 2> >(tee stderr.txt) This just sends stderr to a program running tee. More portably exec 3>&1 foo 2>&1 >&3 | tee stderr.txt This makes file descriptor 3 be a copy of the current stdout (i.e. the screen), then sets up the pipe and runs foo 2>&1 >&3. This sends the stderr of foo to the same place as the current stdout, which is the pipe, then sends the stdout to fd 3, the original output. The pipe feeds the original stderr of foo to tee, which saves it in a file and sends it to the screen. answered Dec 28 '16 at 6:53, icarus Great, that solve my issue! – linuxguy, Dec 28 '16 at 7:25 foo 2>&1 >&3 | tee stderr.txt is working as intended, but I would like to have both to a file and errors to an another. I tried multiple things but nothing do it. Do you have any suggestions? This way, it is easier to locate errors with the real execution by finding them in "both-file" from "error-file". – Master DJon, Jan 23 '18 at 5:08 I found my answer: stackoverflow.com/a/45426547/214898 – Master DJon, Jan 23 '18 at 5:16 @x0a Let me try and explain. The important thing to understand is the way the foo 2>&1 >&3 | tee stderr.txt is executed. The first thing that is handled is the |. To handle this the shell creates a pipe, and then forks to create 2 processes. One handles the foo 2>&1 >&3 and the other handles the tee stderr.txt. The first connects stdout to the pipe, and the second connects stdin to the pipe. .... – icarus, Dec 24 '18 at 23:45 @x0a .... The first shell now does the redirections, left to right. So 2>&1 connects the stderr of what will be foo to the current stdout, i.e. the pipe, then >&1 redirects the stdout of what will become foo )which is currently pointing to the pipe) to the original stdout that was saved in 3 – icarus, Dec 24 '18 at 23:46 +--+ the best answer i could find: here (cmd 2> >(tee dev/stderr)) > log it pipes both (stdout and stderr) to both (screen and file) and let you capture the error if used in inline condition ( if ) or successive inline commands. ex: $((curl "https://unix.stackexchange.com" 2> >(tee /dev/stderr)) >> logfile) && echo "some other commands if curl succeeds without error" in other methods like for piping to tee like that ( | tee ...), the next command ( echo...) is executed whatever (curl) succeeds or fails, because it considers ( tee ) as the last command executed, not ( curl ) answered Jan 3 '20 at 17:23, Enim # enddoc