Bash string handling (char at index and concatenation)

bashstring

I'm trying to learn bash string handling.
How do I create a bash script which is equivalent to this Java code snippet?

String symbols = "abcdefg12345_";
for (char i : symbols.toCharArray()) {
    for (char j : symbols.toCharArray()) {
        System.out.println(new StringBuffer().append(i).append(j));
    }
}

The output of the above code snippet starts with:

aa
ab
ac
ad
ae
af

And ends with:

_g
_1
_2
_3
_4
_5
__

My goal is to have a list of allowed characters (not necessarily the ones above) and print out all the permutations of length 2. If it is possible I would like a solution which relies solely on bash and doesn't require anything else installed.

Edit: Just a little follow up question: Is there a way to do this with a string without spaces separating sub-strings? Like LIST="abcdef12345_"?

Best Solution

That is so simple, Bash does it in the input parser. No code required. Try:

echo {a,b,c,d,e,f,g,1,2,3,4,5,_}{a,b,c,d,e,f,g,1,2,3,4,5,_}

You might need a second pass to split it into lines, though.

Or, you could of course use a couple of nested loops like in your example:

LIST="a b c d e f 1 2 3 4 5 _";
for a in $LIST ; do
    for b in $LIST ; do
        echo $a$b;
    done;
done