Your bash syntax is just fine, although
$(..) is recommended over `..`.
The problem is with your grep regex. It's not matching what you want it to. "
C+", for example, matches a variable-length string of C's, and only C's.
Code:
cars="Audi BMW Cadillac Chevy Dodge Ferrari Ford Mercedes"
list=$( echo ${cars} | egrep -io '\<[AC][^[:space:]]+[[:space:]]' )
Notice first of all how you can combine everything into a single expression. This one matches a word-beginning space, followed by an A or C, followed by one or more non-space characters, followed by a space.
[^x]+x (match anything not-x until you match an x) is a common pattern used for getting around regex greediness, as
* and
+ will keep going until they find the
longest possible match of the expression they effect. If you simply use
.* or
.+, it will match everything to the end of the text.
I also used the
[:space:] character class, which matches space, tab, newline, and a few other rarer characters (read
info grep), but if you know what the input will have, you can use those directly instead.
For case insensitivity, use grep's
-i option.