I know that one can use the $-
contains i or not or check if $PS1
is empty to tell if a shell is interactive.
However, those solutions only work for the current shell.
I have a bash script trying to find the immediate parent shell that is interactive.
For example:
- interactive shell: zsh
- bash script 1 which executes bash script 2
- bash script 2 contains the mechanism to find out the immediate interactive shell
So when we execute bash script 1 under our interactive shell zsh
, we expects an output of zsh
.
I cannot figure out how I can do it, when the script is running in the sub-shell.
Note
I want the script to be executed, not sourced.
Clarify
I have a bash script trying to find the first ancestor shell that is interactive.
By first ancestor, I meant that the first interactive shell we encounter during the bottom-up process-chain scanning process.
For example, in the case of: zsh(first interactive shell) -> bash(second interactive shell) -> bash(batch shell for script 1) -> bash(batch shell for script 2), we want to output bash(the second interactive shell).
sh -cm 'bash script'
? – mikeserv Jul 25 '15 at 05:47