bash:check_if_bash_is_running_in_interactive_mode
This is an old revision of the document!
BASH - Check if bash is running in interactive mode
if [[ $- == *i* ]] then do_interactive_stuff fi
NOTE: According to man bash:
- PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.
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.
Examples
bash <<< 'test -t 0 && echo Y || echo X
writes X
bash -c 'test -t 0 && echo Y || echo X
writes Y
bash/check_if_bash_is_running_in_interactive_mode.1612435374.txt.gz · Last modified: 2021/02/04 10:42 by peter