On 11.03.2018 11:30, Kenny McCormack wrote:
> In article <p82prj$dkh$
1...@news-1.m-online.net>,
> Janis Papanagnou <
janis_pa...@hotmail.com> wrote:
> ...
>> In Ksh, instead of using set and subshell expansion, I find it more
>> convenient to directly read the variables (see below).
>>
> ...
>>
>> lsblk -l -o NAME,SIZE,MOUNTPOINT | ... | read -r Name Size Mountpoint
>> printf "%s\n" "... ${MountPoint:-Not Mounted}"
>
> Alas, in bash (and most sh-like shells), the 'read' statement will be
> executed in a subshell, and thus the values are only seen in that subshell.
> Effectively, the assignments don't get executed at all.
The assignment gets executed but is ineffective in those shells, yes; that
subprocess-effect was the reason why I mentioned Ksh explicitly.
>
> FWIW, while the:
>
> set -- $(...)
>
> method is "portable", I have come to prefer using an array (when
> programming in bash):
>
> myArray=($(...))
This is even possible in many of the modern shells. And set destroys
(redefines) the arg-list which may not be desired.
>
> This has the advantage of not disturbing the passed-in command line args.
>
> P.S. A workaround for the subshell bug is to use a here-document:
Given how widespread that subprocess in pipes behaviour is it's arguable
whether that is to be considered a bug. Some shells put all pipeline
processes in subprocesses while it would also be possible that not the
last (as in Ksh) but, say, the first pipeline command would be executed
in the current shell environment; Stephane has made up an example in the
past (but I don't recall the details). Personally I think it has more
value to have the last pipeline command executed in the current shell
process context. The subprocess "bug" behaviour of other shells is one
of many reasons why I prefer Ksh as my standard shell.
>
> read Name Size Mountpoint << EOF
> $(lsblk -l -o NAME,SIZE,MOUNTPOINT | ...)
> EOF
Janis