home | list info | list archive | date index | thread index

Re: [OCLUG-Tech] how does one test if bash shell is interactive

  • Subject: Re: [OCLUG-Tech] how does one test if bash shell is interactive
  • From: Adrian Irving-Beer <wisq-oclug [ at ] wisq [ dot ] net>
  • Date: Tue, 9 Aug 2005 14:10:21 -0400
On Tue, Jul 26, 2005 at 12:45:33PM -0400, michael ondrechak wrote:

> Is there a way for a bash script to test if it's being run from an
> interactive shell? That is, I want to be able to test whether the
> script was started as:
> 
> $ script
> or as
> $ script &

Actually, if you don't mind me being slightly pedantic, they're both
being run from an interactive shell.  It's just that one is being run
in the background while the other is not.

Now, if you really want to know if your script is being run by a
*user* rather than in the background (by a system script, cron, etc.),
then you can check the result of 

	test -t 0
or
	[ -t 0 ]

to determine if standard input is a terminal.  But running a script as
'script&' isn't enough to make standard input not a terminal; it has to
be something like 

	script < /dev/null&
or 
	nohup script

Anyway, don't know if this is what you're after, but I wanted to make
the distinction.

Attachment: signature.asc
Description: Digital signature