How to iterate over arguments in a Bash script

后端 未结 8 2694
长发绾君心
长发绾君心 2020-11-22 02:27

I have a complex command that I\'d like to make a shell/bash script of. I can write it in terms of $1 easily:

foo $1 args -o $1.ext
         


        
8条回答
  •  独厮守ぢ
    2020-11-22 03:18

    For simple cases you can also use shift. It treats the argument list like a queue. Each shift throws the first argument out and the index of each of the remaining arguments is decremented.

    #this prints all arguments
    while test $# -gt 0
    do
        echo "$1"
        shift
    done
    

提交回复
热议问题