Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
Perfect. Thank you. –  atx Dec 28 '10 at 9:42
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.