[Toybox] Would someone please explain what bash is doing here?

Rob Landley rob at landley.net
Sat May 23 10:11:39 PDT 2020


Starting to open the job control can of worms, and:

  $ while true; do readlink /proc/self | cat - $$; done
  24658
  cat: 20032: No such file or directory
  24660
  cat: 20032: No such file or directory
  24662

Is calling readlink and cat each time through the loop (true is a builtin), so
the pid advances by 2 and the pipeline is NOT a subshell. But:

  $ echo hello | read i; echo $i

The read isn't saved because it's happening in a subshell context (so it sets an
i that is discarded)?

And then:

  $ while true; do continue | readlink /proc/self; done
  28555
  28557
  28559
  28561

Is advancing the pid by 2 each time, because the _continue_ is in its own process?

  $ while true; do continue | cat; echo hello; done
  hello
  hello
  hello

  $ while true; do break | cat; echo hello; done
  hello
  hello
  hello

continue and break are silently NOP in a pipe?

Also, just confirming: $$ only shows the PID of the top level bash process, and
there's no variable that shows the PID of (subshells) even though the point of a
subshell is to spawn a new process?

Because unfortunately "what PID am I" turns out to be legitimately difficult to
answer from a subshell (or at least I couldn't figure it out):

  https://landley.net/notes-2020.html#05-02-2020

Rob

P.S. this is old, but:

  $ for i in a b c & do echo $i; done
  bash: syntax error near unexpected token `&'

But break & is fine? What does that even _mean_?



More information about the Toybox mailing list