bash: executing an output of a script as a script

后端 未结 2 753
长情又很酷
长情又很酷 2020-12-17 00:11

I\'m writing a simple script to generate all combinations of a and b of a given length (say 10). I want to be able to do this on a command line (I know this is fairly easy i

相关标签:
2条回答
  • 2020-12-17 00:56

    You can do for i in $(seq $n) instead of seq 1 1 $n.

    You can do for ((i=1; i<=$n; i++)) and avoid calling an external utility.

    You can do this (slightly hacky with only one loop):

    $ a=A; b=B; n=4; s=''; for ((i=1;i<=n;i++)); do s+="{$a..$b}"; done; eval echo "''" $s"$'\n'"
    

    or this (highly hacky without any loops):

    $ a=A; b=B; n=4; eval echo "''" $(printf "{$a..$b}%.0s" $(eval echo "{1..$n}"))"$'\n'"
    

    Either one will get you this:

     AAAA
     AAAB
     AABA
     AABB
     ABAA
     ABAB
     ABBA
     ABBB
     BAAA
     BAAB
     BABA
     BABB
     BBAA
     BBAB
     BBBA
     BBBB
    
    0 讨论(0)
  • 2020-12-17 01:03

    You need to use an eval, $() gives you a string.

    eval $( echo echo foo )
    

    Another option is to stick into a subshell and pipe it to a bash:

    (echo echo foo) | /bin/bash
    
    0 讨论(0)
提交回复
热议问题