Bash Process Substitution

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tanvalley
    New Member
    • Jun 2012
    • 17

    Bash Process Substitution

    In this sequence at the command prompt
    Code:
    $ x=()
    $ { while read;  do  x+=("$REPLY"); done; } <<<"$(ls)"
    $ printf 'count=%d\n' ${#x[@]}
    count=432
    $ wc -l <(ls)
         432 /dev/fd/63
    $ 
    $ 
    $ { while read;  do  x+=("$REPLY"); done; } <(ls)
    -bash: syntax error near unexpected token `<(ls)'
    what causes the error noted?

    Doing
    Code:
    $ x=()
    $ ls | { while read; do x+=("$REPLY"); done }
    $  printf 'count=%d\n' ${#x[@]}
    count=0
    $
    $
    runs without error, but executes the loop in a subprocess, losing the results on termination
    BASH 3.2.57(1)-release on OS X 10.10.5
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Have you tried adding a $ before the (ls), like so:

    Code:
    { while read;  do  x+=("$REPLY"); done; } < $(ls)

    Comment

    Working...