PS1 is also a common variable to check.

if [ -z $PS1 ] # no prompt?
then
  # non-interactive
  ...
else
  # interactive
  ...
fi



On Mon, 07 Jan 2002, Chad C. Walstrom wrote:

> Here's a quick BASH tip for everyone who uses it.  This involves the difference
> between interactive shells and non-interactive.  Have you ever had scp(1) fail
> on you because of a command you put in your .bash_profile?  Let's say you
> called uptime(1) or who(1).
> 
> bash$ scp me at myhost:myfile .
>  10:47am  up 55 days, 16:14, 31 users,  load average: 0.01, 0.03, 0.00
> bash$ ls myfile
> ls: myfile: No such file or directory
> 
> OK.  Here's the problem.  scp(1) reads your .bash_profile and barfs on any
> stdout that shouldn't normally be there.  In my profile, I have lines such as:
> 
>     test [ -f /etc/profile ]  && source /etc/profile
>     test [ -f ~/.bashrc ] && source ~/.bashrc
> 
> In my .bashrc, I've called uptime(1):
>     
>     uptime
> 
> Luckily, there's a simple fix.  You can examine the options that the bash shell
> has set by default, or that you have set manually, by examining the $-
> variable.
> 
>     # example ~/.bashrc
>     do_interactive() {
>         uptime
>         who
>         mesg y
>     }
> 
>     case $- in
>         *i*)
>             do_interactive
>         ;;
>     esac
> 
>     # ... your standard noninteractive rc stuff follows
> 
> You can use this test at any time: .bash{_profile, _login,rc}, .profile, etc.
> Make sure you read up on BASH in the manpages and understand the order of the
> resource control files for the shell when it's executed.
> 
> -- 
> Chad Walstrom <chewie at wookimus.net>                 | a.k.a. ^chewie
> http://www.wookimus.net/                            | s.k.a. gunnarr
> Get my public key, ICQ#, etc.  Send email w/the Subject: "get help"
> _______________________________________________
> Twin Cities Linux Users Group Mailing List - Minneapolis/St. Paul, Minnesota
> http://www.mn-linux.org
> tclug-list at mn-linux.org
> https://mailman.mn-linux.org/mailman/listinfo/tclug-list