Test if login is an scp connection

I'm echo'ing various machine statistics on login, but this is problematic for SCP and SFTP, is there a shell variable I can test for?


In bash, I use shopt -q login_shell to test for that. For example in .bashrc:

if shopt -q login_shell
then
    echo "interesting stuff"
fi

This should keep the "interesting stuff" out of your scp/sftp.


According to the man page, you should test for the presence of "i" in $-.

PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

For example:

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

Historically, in cleanly configured bourne-style shells, it's "test if PS1 is set", but that's broken if some joker exports PS1 to the environment.

The SUS standards-compliant method is to test if 'i' is in $-, as Dennis notes, although [[ ... ]] is non-standard, as is the == comparator. So the most portable standards-compliant check is:

case $- in
 *i*) # do interactive stuff
  ;;
esac

Then you have shopt -q login_shell for bash (per Cakemox), and both [[ -o interactive ]] and [[ -o login ]] for zsh.