Search code examples
bashuser-input

How can a script know whether a user can input or not?


Is there a way a bash script (or whatever language) can determine whether its being run in a headless way? I want to know whether a user can input or not. If they can, I am going to ask them something.


Solution

  • From man bash:

    An  interactive  shell  is  one  started without non-option arguments and without the -c option whose standard input and
           error are both connected to terminals (as determined by isatty(3)), or one started with the -i option.  PS1 is  set  and
           $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.