You have some code you want to run only if you are (or are not) running interactively.
Use the following case statement:
1 2 3 4 5 6 7 8 9 |
#!/usr/bin/env bash # cookbook filename: interactive case "$-" in *i*) # Code for interactive shell here ;; *) # Code for non-interactive shell here ;; esac |
$- is a string listing of all the current shell option flags. It will contain i if the shell is interactive.
You may also see code like the following (this will work, but the solution above is the preferred method):
1 2 3 4 5 |
if [ "$PS1" ]; then echo This shell is interactive else echo This shell is not interactive fi |