0

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.

  • So, if the current shell's parent process is an interactive shell or not? – Kusalananda Jul 1 '16 at 15:31
  • Not sure I understand what you're asking. Does this solve your problem: if expr match "$-" '.*i' >/dev/null; then echo interactive; fi ? – Satō Katsura Jul 1 '16 at 19:43
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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