From within a shell script (sh/bash), how can I tell what device or file name I am redirecting to?For example, I have a shell script named script.sh.I run the shell script and redirect STDOUT to a file named out.txt, and redirect STDERR to a file named err.txt.So, that would look like this:$ ./script.sh > out.txt 2> err.txtNow, from within the shell script, I can tell if STDOUT (or STDERR) is being redirected, or not:if [ -t 1 ]; then...if [ ! -t 1 ]; then...but, once I know STDOUT (or STDERR) is being redirected, how can I tell where it is being rdirected to?
How can I tell what device or file name I am redirecting to
shell script;io redirection
You could use lsof:/usr/sbin/lsof -p $$ -a -d 1The above would list just file descriptor 1 for just the current process:COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAMEbash    5054 matt    1u   CHR  136,1      0t0    4 /dev/pts/1