Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether stderr is a pipe in bash

Tags:

bash

stderr

pipe

I have a bash script that prompts the user for input with 'read'. If stdout or stderr is piped to something other than a terminal, I would like to suppress this step. Is that possible?

like image 368
A B Avatar asked Jul 29 '09 21:07

A B


1 Answers

You can check whether a filedescriptor is a tty(attached to a terminal) with the command test -t <filedescriptor no.>. If it is, you can prompt the user. If it isn't, output is probably piped or redicted somewhere.

if test -t 1  ; then
  echo stdout is a tty
fi
like image 126
nos Avatar answered Oct 05 '22 22:10

nos