Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 7 down vote accepted

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

if shopt -q login_shell
    echo "interesting stuff"

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

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* ]]
    # do interactive stuff
share|improve this answer

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

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

share|improve this answer

Your Answer


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.