0

I seem to remember some command or envvar that tells you this. I'd like basically something that executed on terminal, for instance, give you

echo $STDIN 
/dev/tty
echo $STDOUT
/dev/tty
fdescribe 0 or fdescribe /dev/stdin
/dev/tty
fdescribe 1 or fdescribe /dev/stdout
/dev/tty

but that can be executed in a script or subprocess

1 Answer 1

4

Well, fd 0 is associated to stdin and fd 1 to stdout by definition.

If you want to know what files are opened on those fds in a (Bourne-like or csh-like¹) shell, you can do:

lsof -ap "$$" -d 0-1

On Linux or Cygwin, see also:

realpath /dev/stdin /dev/stdout

Though the output is less useful if it's sockets or pipes.

On Linux again, adding -E to lsof gives more information for some types of files such as pipes, pseudo-ttys or some sockets as to what's at the other end if any.

Recent versions of util-linux also have a lsfd utility (Linux-specific) that can give you some of the same information as lsof:

lsfd -p "$$" -Q '(fd == 0) or (fd == 1)'

Or

lsfd -p "$$" -Q 'assoc =~ "^[01]$"'

¹ in rc-like shells use $pid instead of $$ and in fish: $fish_pid

1
  • Looks like I mixed up my memories. I was looking indeed for $TTY envar. But it is not what I need now, what i needed to attack my problem is your brilliant answer. Thanks again!!!!
    – Whimusical
    Commented Feb 25 at 21:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .