Quote:Original post by BeauMN
${SOURCE} will never be defined in the case statement because the case statement is evaluated before the for loop is run? But on the other hand, if you have an if statement inside the for loop, the if statement will get reevaluated on each iteration of that for loop?
After further investigation I figured out what the problem is.
As the bash documentation describes, variable substitution takes place when the case statement is evaluated. In other words, both ${SOURCE} and ${SOURCES} are expanded as you expect.
Problem is, this is done at evaluation time, not at parse time. The parser will tokenize the match pattern into the various alternatives
before the variable substitution takes place.
Consider a construct like this.
matchme="a | b" case a in $matchme) echo "never gets here" ;; *) echo "always gets here" ;; esac
There will be two patterns to match against: "a | b" and "*". Note the first pattern is not the same as "a" | "b" but instead consists of a 5-character token.
Unfortunately, try as I might, I seem unable to work around the dynamic construction of alternate cases. I stand by my previous assertion that you just need to come up with a simpler way of traversing your list.
--smw