Search code examples
environment-variablesinteractiveshsubshell

How can I tell from a within a shell script if the shell that invoked it is an interactive shell?


I'm trying to set up a shell script that will start a screen session (or rejoin an existing one) only if it is invoked from an interactive shell. The solution I have seen is to check if $- contains the letter "i":

#!/bin/sh -e

echo "Testing interactivity..."
echo 'Current value of $- = '"$-"
if [ `echo \$- | grep -qs i` ]; then
  echo interactive;
else
  echo noninteractive;
fi

However, this fails, because the script is run by a new noninteractive shell, invoked as a result of the #!/bin/sh at the top. If I source the script instead of running it, it works as desired, but that's an ugly hack. I'd rather have it work when I run it.

So how can I test for interactivity within a script?


Solution

  • Give this a try and see if it does what you're looking for:

    #!/bin/sh
    if [ $_ != $0 ]
    then
      echo interactive;
    else
      echo noninteractive;
    fi
    

    The underscore ($_) expands to the absolute pathname used to invoke the script. The zero ($0) expands to the name of the script. If they're different then the script was invoked from an interactive shell. In Bash, subsequent expansion of $_ gives the expanded argument to the previous command (it might be a good idea to save the value of $_ in another variable in order to preserve it).

    From man bash:

       0      Expands to the name of the shell or shell script.  This  is  set
              at shell initialization.  If bash is invoked with a file of com‐
              mands, $0 is set to the name of that file.  If bash  is  started
              with  the  -c option, then $0 is set to the first argument after
              the string to be executed, if one is present.  Otherwise, it  is
              set  to  the file name used to invoke bash, as given by argument
              zero.
       _      At shell startup, set to the absolute pathname  used  to  invoke
              the  shell or shell script being executed as passed in the envi‐
              ronment or argument list.  Subsequently,  expands  to  the  last
              argument  to the previous command, after expansion.  Also set to
              the full pathname used  to  invoke  each  command  executed  and
              placed in the environment exported to that command.  When check‐
              ing mail, this parameter holds the name of the  mail  file  cur‐
              rently being checked.