You can check if the shell is interactive or not by checking the value of the -o
option of the PS1
environment variable:
if [ "${-#*i}" != "$-" ]; then
echo "Interactive shell"
else
echo "Not an interactive shell"
fi
In this example, "${-#*i}"
expands to the value of the $-
variable with the first occurrence of the i
character removed. If the shell is interactive, the i
character will be present, and the expansion will be different from the original value of $-
. If the shell is not interactive, the i
character will not be present, and the expansion will be the same as the original value of $-
.