Solaris 10 / bin / sh forks indirectly

I noticed that the Solaris 10 Bourne shell, /bin/sh (as well as /sbin/sh ) expands the subshell when using indirectness ( < ). I tried a bunch of other Bourne-ish shells, including:

  • POSIX /usr/xpg4/bin/sh shell on Solaris 10
  • /bin/bash , /bin/ksh on Solaris 10
  • /bin/sh on AIX 5
  • /bin/sh on Debian Linux 5

and not one of them exhibits such behavior.

I am surprised that I have not bitten this before. For example, in more transparent shells (i.e., all of the above), the following script outputs "1":

 $ cat foo #!/bin/sh x=0 while read y ; do x=1 done </etc/passwd echo $x $ ./foo 0 $ 

Solaris 10 /bin/sh returns 0 because the assignment x=1 occurs in a subshell caused by indirectness: when the subshell completes this assignment. (If I remove </etc/passwd and read from stdin , then "1" comes out, as expected).

Is there any age-old reason that this "traditional" Solaris sh this property? Or is this a mistake?

+4
source share
2 answers

I would say that this is a violation of the POSIX standard.

Substitution substitution, commands that are grouped with parentheses and asynchronous lists must be performed in a subnet environment. In addition, each pipeline command with multiple commands is in a subnet environment; however, as an extension, any or all of the commands in the pipeline can be executed in the current environment. All other commands must be executed in the current shell environment.

Source: Shell Command Language , Section 2.12.

+1
source

The bourne shell does this — create a child process — for loops and other constructs. Expected Behavior. This is not a mistake in the sense that anyone who uses it “knows” this problem exists. Sometimes there is a bad assumpotion.

DO NOT DEVELOP in Bourne, except for system scripts (example: start / shutdown) in Solaris. Instead, use the POSIX shell: ksh, bash. By default, the root shell should be a Bourne shell if it will not be loaded by the system. This is an artifact of the old system V.

The same kind of caution exists for encoding in csh. He is also not ready for prime time.

+1
source

Source: https://habr.com/ru/post/1315782/


All Articles