Is there a better way to run a command N times in bash?

后端 未结 19 2418
执笔经年
执笔经年 2020-11-28 00:26

I occasionally run a bash command line like this:

n=0; while [[ $n -lt 10 ]]; do some_command; n=$((n+1)); done

To run some_command

19条回答
  •  情话喂你
    2020-11-28 01:30

    For one, you can wrap it up in a function:

    function manytimes {
        n=0
        times=$1
        shift
        while [[ $n -lt $times ]]; do
            $@
            n=$((n+1))
        done
    }
    

    Call it like:

    $ manytimes 3 echo "test" | tr 'e' 'E'
    tEst
    tEst
    tEst
    

提交回复
热议问题