views:

434

answers:

1

What's the preferred way to determine if a given ksh invocation is running an interactive shell?

I have some commands in an ENV file that I would like to skip for non-interactive ksh invocations (e.g. when executing a shell script).

I've seen suggesting ranging from:

if [[ $- = *i* ]]; then
    # do interactive stuff
fi

...to not even sourcing .kshrc unless the shell is determined to be interactive using this cryptic incantation:

ENVIRON=$HOME/.kshrc                                    export ENVIRON
ENV='${ENVIRON[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'       export ENV
+1  A: 

In bash, these two methods are often used inside ~/.bashrc:

  • Check if stdin is a tty:

    [ -t 0 ] || return
    

    or

    if [ -t 0 ]; then
        # do interactive stuff
    fi
    
  • Check if the prompt ($PS1) is set:

    [ -z "$PS1" ] || return
    

But I don't know how to do that in ksh.

grawity
The '-t N' tests work in Korn shell too.
Jonathan Leffler
Is '-t N' definitive? Are there cases where it differs from '$- = *i*'?
David Citron