Friday, September 16, 2005

check if the shell is interactive or not in the profile

On Sun, 11 Sep 2005 10:08:49 +0200, <n.att...@tiscali.it> wrote:
> Barry,
> how can I check if the shell is interactive or not in the profile?

What I say here applies to the Bourne Again shell (bash), but I believe
ksh is similar:

PS1 is set and $- includes an 'i' if the shell is interactive.

if [ "${PS1+set}" = set ]
then echo interactive
else echo non-interactive
fi

and

case $- in
*i*) echo interactive;;
*) echo non-interactive;;
esac

or more briefly:

[[ $- =~ i ]] && echo interactive || echo non-interactive

The shell will be interactive if

a) both stdin and stdout is connected to a terminal,
b) the shell was not started with non-option command
line arguments, and
c) the shell was not started with the -c option.

So,

if [[ -t 0 -a -t 1 -a $# -eq 0 -a $- =~ c ]]
then
echo "Yessur, a, b, and c."
fi

since the "=~" operator exists in [[...]] but not in [...].
People are often happy to test only [ -t 0 ], which works as
intended in most cases.

> Is it something about the tty?

Yes.

-Enrique

0 Comments:

Post a Comment

<< Home