-2

I am accidentally caught in a desire to reveal the subshell number (BASH_SUBSHELL) from within the script itself, but i get subshell 0

Here is the script's line

echo "Operated from subshell: $BASH_SUBSHELL

Part of the shell's output in terminal

  • echo 'Operated from subshell: 0' Operated from subshell: 0

Question Is it possible to reveal the subshell a script is operating from within the script itself?

  • 1
    Scripts don't run in subshells, so this output looks correct to me. What are you expecting? – Michael Homer Apr 23 '19 at 21:27
  • @jim-chriss, Michael is right: executing a shell script runs that script is a separate process, not as a subshell of your interactive shell. – glenn jackman Apr 23 '19 at 21:31
  • @Michael Homer shell scripts do run in subshells if invoked using their names in terminal, I quote the tldp book page 28, says "This is the most common way to execute a script. It is preferred to execute the script like this in a subshell." – Jim-chriss Charles Apr 24 '19 at 08:25
  • No, they don't. – Michael Homer Apr 24 '19 at 08:27
  • I will acknowledge that the book [does say that](https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html), and that it does so because the book is incompetently written. – Michael Homer Apr 24 '19 at 08:29
  • Refer [Is a sub-shell the same thing as a child-shell?](https://unix.stackexchange.com/q/261638/73093) (I feel like we have a better version of that question, but I can't find it at the moment) – Michael Homer Apr 24 '19 at 08:32
  • Maybe I should take that the book is incompetently written because I thought this was written by the Linux benevolent patrons themselves. – Jim-chriss Charles Apr 24 '19 at 08:36
  • Do you mind if I ask something @MichaelHomer, Does the notion **subshell** mean a child process of the current running shell session? Because, I inserted a sleep 1m in my script and hit Ctrl-Z (to stop job), after running `ps -f` the PID of the shell (i.e bash ) was exactly the same as the PPID (Parent's PID) of `bash script.sh` . Doesn't this prove that a running shell script runs in it's own subshell? – Jim-chriss Charles Apr 24 '19 at 15:54
  • See also [What is the exact difference between a “subshell” and a “child process”?](https://unix.stackexchange.com/q/421020/73093) in addition to [Is a sub-shell the same thing as a child-shell?](https://unix.stackexchange.com/q/261638/73093). Scripts are not run in subshells. – Michael Homer Apr 24 '19 at 21:00
  • Look at this @MichaelHomer. The Bash Reference Manual sub-category **3.8 Shell Scripts** says, "A shell script may be made executable by using the chmod command to turn on the execute bit. When Bash finds such a file while searching the $PATH for a command, it spawns a subshell to execute it. In other words, executing `filename _arguments_` is equivalent to executing `bash filename _arguments_`. if `filename` is an executable shell script. This subshell reinitializes itself, so that the effect is as if a new shell had been invoked to interpret the script,... – Jim-chriss Charles Apr 25 '19 at 16:27

2 Answers2

0

Here's some code that actually uses some subhells:

echo "main shell: $BASH_SUBSHELL"
( 
    echo "first subshell: $BASH_SUBSHELL"
    ( 
        echo "second subshell: $BASH_SUBSHELL"
        (
            echo "third subshell: $BASH_SUBSHELL"
        )
    )
)
glenn jackman
  • 84,176
  • 15
  • 116
  • 168
0

Since scripts do not run in subshells, this output is correct. Subshells are created by a few things, including parentheses ( ... ), backgrounding with &, and command substitution $( ... ), but not by launching scripts: that creates a whole new shell to execute the script in.

What you may be thinking of is the SHLVL variable, which does increment for each layer of script (and shell):

SHLVL Incremented by one each time a new instance of Bash is started. This is intended to be a count of how deeply your Bash shells are nested.

If your script line were

echo "Operated from shell level: $SHLVL"

then you would get the output that I think you expected. If the script recursed, $SHLVL would increment each time.

Michael Homer
  • 74,824
  • 17
  • 212
  • 233
  • Bash Guide for Beginners, on page 28, says invoking a script as `willy:~> script1.sh` is ... I quote "This is the most common way to execute a script. It is preferred to execute the script like this in a subshell. The variables, functions and aliases created in this subshell are only known to the particular bash session of that subshell. When that shell exits and the parent regains control, everything is cleaned up and all changes to the state of the shell made by the script, are forgotten." – Jim-chriss Charles Apr 24 '19 at 08:28