bash shell nested for loop

后端 未结 3 1152
轻奢々
轻奢々 2021-01-31 03:04

I want to write a nested for loop that has to work in the bash shell prompt. nested for loop in Single line command.

For example,

for i in a b; do echo         


        
3条回答
  •  终归单人心
    2021-01-31 03:59

    One one line (semi-colons necessary):

    for i in 0 1 2 3 4 5 6 7 8 9; do for j in 0 1 2 3 4 5 6 7 8 9; do echo "$i$j"; done; done
    

    Formatted for legibility (no semi-colons needed):

    for i in 0 1 2 3 4 5 6 7 8 9
    do
        for j in 0 1 2 3 4 5 6 7 8 9
        do 
            echo "$i$j"
        done
    done
    

    There are different views on how the shell code should be laid out over multiple lines; that's about what I normally use, unless I put the next operation on the same line as the do (saving two lines here).

提交回复
热议问题