if [[ $- == *i* ]] then do_interactive_stuff fi if [ "$-#*i" == "$-" ]; then ... fi
NOTE: According to man bash:
if [ -t 0 ] ; then echo stdin is a terminal ..... fi
or
if [ -t 1 ] ; then echo stdout is a terminal fi
NOTE: -t FD is True if FD is opened on a terminal.
Portable between shells.
bash <<< 'test -t 0 && echo Y || echo X
writes X
bash -c 'test -t 0 && echo Y || echo X
writes Y
NOTE: This also verifies that the standard input is a TTY; although it can be related but it is NOT the same as the shell's interactive mode, which is requested and indicated by shell's ā-iā flag.