How to fork shell to foreground instead of background?

335 views Asked by At

I want to use a shell script to check if it is interactive. If so, it should spawn a fish and continue the rest of the script in the background. Otherwise, it should not run fish and continue running the rest of the script.

How I check if the shell is interactive:

#!/bin/sh

case $- in
    *i*) echo "interactive" ;;
    *) echo "non-interactive" ;;
esac

Now, I want to spawn a different shell in the foreground if it is interactive, and continue to run the rest of the script in the background.

In this example, it will wait until I exit fish to run echo "continue....

#!/bin/sh

case $- in
    *i*) fish ;;
    *) ;;
esac

echo "continue..."

Things I tried:

  • If I use fish &, the rest of the script runs in the foreground and fish runs in the background.
  • If I use fish, the script runs fish in the foreground, but only continues the rest of the script once the fish shell is exits.
  • If I use exec fish, the script runs fish in the foreground, but does not run the rest of the script, because fish replaces the parent.

Note that I am using dash and not bash for the script, so it should not contain any bashisms.

0

There are 0 answers