Why does a subshell of an interactive shell run as an interactive shell?

Viewed 477

With my Bash session, I run this command:

(echo $$ $BASHPID $-)

and I get

22108 25602 himBH

So, my subshell runs as an interactive shell.

If I try to run the same command in background

(echo $$ $BASHPID $-) &

I get the same output. Why does the subshell run as an interactive shell?

1 Answers

A subshell inherits all the characteristics of the parent, and that includes settings. That's part of the definition of a subshell. It makes no difference if it is running in foreground or background - jobs can be swapped between foreground and background quite easily.

A non-subshell will not be interactive. For example, if I put

(echo $$ $BASHPID $-)

into a script called gash.sh and call it from a non-interactive shell:

$ bash gash.sh
73879 73880 hB

But if I source the file, then it is a subshell of an interactive shell:

$ . ./gash.sh
1130 himBH
Related