Search code examples
bashloopsfor-loopbrace-expansion

Loop through different variations of a word


I would like to loop through different variations of a word, but can't think of how the syntax would be arranged, for example:

for host in server(01,02,03,04)db
    do echo $host
done

would return:

server01db
server02db
server03db
server04db

How would I structure that loop?


Solution

  • Use curly braces instead of parenthesis:

    for host in server{01,02,03,04}db 
    do
      echo "$host"
    done
    

    Look at man bash for Brace Expansion