Bash string processing (char by index and concatenation)

I am trying to learn bash string handling. How to create a bash script that is equivalent to this piece of Java code?

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:

_g
_1
_2
_3
_4
_5
__

My goal is to have a list of valid characters (not necessarily higher) and print all permutations of length 2. If possible, I would like to get a solution that relies solely on bash and does not require anything else.

Edit: Just a small question: is there a way to do this with a string without spaces separating the substrings? How is LIST = "abcdef12345 _"?

+3
source share
4 answers

, Bash . . :

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,_}

, .

, , , :

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
+5
for i in a b c d e f g 1 2 3 4 5 _; do
    for j in a b c d e f g 1 2 3 4 5 _; do
        echo $i$j
    done
done

man bash - . .

0

Conveniently for one line:

 for i in `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,_}`; do echo $i; done
0
source

Only a few options on how you can separate the created elements. Personally, I like to use trfor this assignment.

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,_} | tr " " "\n"
0
source

All Articles