logoalt Hacker News

skydhashyesterday at 3:03 PM1 replyview on HN

There’s a function isatty that detect if a file descriptor (stdout is one) is associated with a terminal

https://man.openbsd.org/man3/ttyname.3

I believe most standard libraries has a version.


Replies

sdsdyesterday at 3:14 PM

I was about to comment the same thing. Usually I don't call the function directly, but via the tty command in my shell scripts:

  if tty -s; then
    echo "Standard input is a TTY (interactive mode)."
  else
    echo "Standard input is not a TTY (e.g., piped or redirected)."
  fi
Now I wonder how _isatty_ itself detects whether a file descriptor is associated with a terminal!
show 1 reply