Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating variations with bash

Tags:

bash

Looking for en nicer solution for the next code:

for i in a b c
do
  for j in A B C
  do
    for k in 1 2 3
    do
      echo "$i$j$k"
    done
  done
done

Sure is here some simpler solution.

like image 699
cajwine Avatar asked Sep 23 '12 20:09

cajwine


2 Answers

This is a bit simpler

echo {a,b,c}{A,B,C}{1,2,3}

or if want one per line, so

echo {a,b,c}{A,B,C}{1,2,3} | xargs -n1

BTW, you can use the above bracer expansion for example saving keyboard typing for example when need make backup files, like:

cp /some/long/path/And_very-ugly-fileName{,.copy}

will make the /some/long/path/And_very-ugly-fileName.copy without second filename typing.

like image 115
jozkov99 Avatar answered Nov 08 '22 13:11

jozkov99


Use a brace expansion:

echo {a,b,c}{A,B,C}{1,2,3}

This will print all possible combinations between the sets {a,b,c}, {A,B,C}, and {1,2,3}.

Or even simpler, using ranges:

echo {a..c}{A..C}{1..3}

And if you want to print each combination per line, use a for-loop:

for i in {a..c}{A..C}{1..3}; do echo $i; done
like image 32
João Silva Avatar answered Nov 08 '22 12:11

João Silva