Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I noticed in BASH's list of CONDITIONAL EXPRESSIONS a -t expression that tests whether a file descriptor is an open terminal. i tried the following:

if [[ -t $(tty) ]]; then
  echo open terminal $(tty)
  echo not open terminal $(tty)

and get my response

not open terminal /dev/pts/35

i've tried the same from a console and get

not open terminal /dev/tty1

in the man page -t describes its argument as FD a "file descriptor" rather than other conditional expressions which describe the argument as FILE

can anyone provide a successful BASH test for a 'file descriptor' which is "open" and "refers to terminal"?

share|improve this question
up vote 1 down vote accepted

File descriptors are small integers.

In particular, stdin is fd 0; stdout is fd 1; and stderr is fd 2.

You will have seen the same use of "file descriptor" in lines like:

./cmd 2>&1

Which means "run ./cmd, redirecting fd 2 (stderr) to fd 1 (stdout).

The normal use of -t is to tell if input is coming from a terminal:

[[ -t 0 ]] && echo "Input is coming from a terminal"
share|improve this answer
thanks for pointing me in the right direction. with the your tip i started exploring /dev/fd (which is a symlink to /proc/self/fd). And then with I was able to create arbitrary file descriptors and make them persistent across commands (using 'exec 6>~/filename' or 'exec 7>/dev/pts/1'). Using these I was able to determine that '-t' does test positively for any arbitrary fd pointed to any arbitrary terminal. so 'exec 13>/dev/tty3; [[ -t 13 ]] && echo "open term"' gave me the output of "open term" :) – ben_wing Nov 26 '13 at 22:29

You must log in to answer this question.

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