Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I have a question on using bash's bracket expansion on the two sets {$SET1} and {$(ls)}. Why doesn't the following work in bash and how else might this be ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    2

    Bash bracket expansion as a Direct Product.

    Hello, I have a question on using bash's bracket expansion on the two sets {$SET1} and {$(ls)}. Why doesn't the following work in bash and how else might this be done short of recursion or nested loops, in bash or another language?
    Code:
    > SET1=a,b,c
    > echo {$SET1}{3,4};
    {a,b,c}3 {a,b,c}4
    Thanks in advanced,
    Eric.

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Welcome to the forums.

    This script shows some of the possibilities:
    Code:
    #!/bin/bash -
    
    # @(#) s1       Demonstrate bash brace expansion.
    
    echo
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
    set -o nounset
    
    echo
    echo " Results, constants:"
    echo {a,b,c}{3,4}
    
    echo
    echo " Results, variable:"
    SET1=a,b,c
    echo {$SET1}{3,4}
    
    echo
    echo " Results, variable + eval:"
    SET1=a,b,c
    eval echo {$SET1}{3,4}
    
    exit 0
    Producing:
    Code:
    % ./s1
    
    (Versions displayed with local utility "version")
    Linux 2.6.11-x1
    GNU bash 2.05b.0
    
     Results, constants:
    a3 a4 b3 b4 c3 c4
    
     Results, variable:
    {a,b,c}{3,4}
    
     Results, variable + eval:
    a3 a4 b3 b4 c3 c4
    The expansion does not work as you wish because of the order in which various expansions are performed:
    The order of expansions is: brace expansion, tilde expansion, parame-
    ter, variable and arithmetic expansion and command substitution (done
    in a left-to-right fashion), word splitting, and pathname expansion.

    -- excerpt from man bash
    so we need to have the line scanned more than once. For that, we use eval.

    There is a entire section on brace expansion in the man page; the quote is just above that section.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Dec 2008
    Posts
    2
    That great! I was afraid it wouldn't be possible.

    Thanks again for replying.
    Eric.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...