4

It's fairly straightforward to determine from within a script that the code being run is not running in an interactive shell, but you already know that as you write the script, so I'm not sure how it's useful except within a function (or if you source the script).

Within a script, how do you determine if the script is being run from an interactive shell, or being run by another script? In other words, how do you determine the interactivity of the caller of the script? Put yet another way, how do you know whether the parent shell is interactive or not?

Just to further clarify, I'm not trying to find the name of the nearest interactive ancestor, I'm just trying to figure out if the immediate parent is interactive or not.

If this varies from shell to shell, I'm most interested in zsh, but also in bash.

iconoclast
  • 9,198
  • 13
  • 57
  • 97

1 Answers1

1

If the script starts with #!/path, then the kernel starts a new unrelated shell.

If the script contains just shell commands, the shell forks, gets an ENOEXEC error, resets all flags to their defaults and then the forked child runs the script.

So you cannot except when you could use something similar to getppid() and check the process table.

schily
  • 19,173
  • 1
    I've asked a somewhat related but much more detailed and involved question that I would love your input on (as a shell maintainer): http://unix.stackexchange.com/q/339506/135943 – Wildcard Jan 23 '17 at 11:22