Bash Check if Shell on Interactive or Login
In Bash, you can check whether the shell is running in an interactive or login mode using the provided commands. Here’s an explanation of each command and what it checks for:
-
[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive':- This command checks the value of the special shell variable
$-, which contains a string of options and flags that are currently set for the shell. - The
*i*pattern is used to check if the letter ‘i’ appears anywhere in the value of$-. If it does, it indicates that the shell is running in interactive mode. - If ‘i’ is found, it echoes ‘Interactive’, otherwise, it echoes ‘Not interactive’.
- This command checks the value of the special shell variable
-
shopt -q login_shell && echo 'Login shell' || echo 'Not login shell':






