[Toybox] hostname vs ${x at P}

Chet Ramey chet.ramey at case.edu
Sat Sep 26 19:00:52 UTC 2026


On 9/24/26 6:48 PM, Rob Landley wrote:

> Did I ever ask why this happens?

The short answer is that the extra newline turns the command from a simple
command into a list (of sorts).

> $ env -i bash --noprofile --norc -c env
> PWD=/home/landley/toybox/clean
> SHLVL=0
> _=/usr/bin/env
> $ env -i bash --noprofile --norc -c $'env\n\n'
> PWD=/home/landley/toybox/clean
> SHLVL=1
> _=/usr/bin/env
> 
> It's testing the default exports, but without the two newlines in -c bash 
> says SHLVL=0. (I'm going through sorting old tests into a more coherent 
> order to see what I'm not testing yet, and... why?)

The simple command allows bash to optimize away the fork, which it can't
do when the simple command is not the final command in the string.

When bash optimizes out the fork, which basically adds an implicit `exec'
at the beginning, it decrements SHLVL in the environment the command sees,
just like the `exec' builtin would do. `exec' does this so something like
`exec bash' preserves the value of $SHLVL as one would expect.
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet at case.edu    http://tiswww.cwru.edu/~chet/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 203 bytes
Desc: OpenPGP digital signature
URL: <http://lists.landley.net/pipermail/toybox/attachments/20260926/170b656a/attachment.sig>


More information about the Toybox mailing list