I'm pretty sure that the "illegal variable name" is the "$(2)" in your sub-brick selector "[0..$(2)]". Put this in 'single quotes' (and also change "[1..$(2)]" to '[1..$(2)]') and try again. The $subj is OK.
Inside "double quotes", the shell expands $variablename -- but you can't have a variable named '(2)'. Inside 'single quotes', everything is passed through without shell interpretation. So unless you are actually using shell variables inside a sub-brick selector, the safest thing to do is use single quotes.