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_’?
That is so simple, Bash does it in the input parser. No code required. Try:
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: