User Tools

Site Tools


bash:check_if_bash_is_running_in_interactive_mode

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
bash:check_if_bash_is_running_in_interactive_mode [2021/02/04 10:38] – created peterbash:check_if_bash_is_running_in_interactive_mode [2021/02/04 11:08] (current) – [BASH - Check if bash is running in interactive mode] peter
Line 5: Line 5:
 then then
   do_interactive_stuff   do_interactive_stuff
 +fi
 +
 +
 +if [ "$-#*i" == "$-" ]; then
 +  ...
 fi fi
 </code> </code>
Line 15: Line 20:
 </WRAP> </WRAP>
  
 +----
 +
 +====== Check if standard input is a TTY ======
 +
 +<code bash>
 +if [ -t 0 ] ; then
 +  echo stdin is a terminal
 +  .....
 +fi
 +</code>
 +
 +or
 +
 +<code bash>
 +if [ -t 1 ] ; then
 +  echo stdout is a terminal
 +fi
 +</code>
 +
 +<WRAP info>
 +**NOTE:**   **-t FD** is True if FD is opened on a terminal.
 +
 +Portable between shells.
 +</WRAP>
 +
 +----
 +
 +===== Examples =====
 +
 +<code bash>
 +bash <<< 'test -t 0 && echo Y || echo X
 +</code>
 +
 +writes X
 +
 +<code bash>
 +bash -c 'test -t 0 && echo Y || echo X
 +</code>
 +
 +writes Y
 +
 +<WRAP info>
 +**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.
 +</WRAP>
 +
 +
 +----
bash/check_if_bash_is_running_in_interactive_mode.1612435119.txt.gz · Last modified: 2021/02/04 10:38 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki