How to iterate over arguments in a Bash script

后端 未结 8 2690
长发绾君心
长发绾君心 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:08

    Use "$@" to represent all the arguments:

    for var in "$@"
    do
        echo "$var"
    done
    

    This will iterate over each argument and print it out on a separate line. $@ behaves like $* except that when quoted the arguments are broken up properly if there are spaces in them:

    sh test.sh 1 2 '3 4'
    1
    2
    3 4
    

提交回复
热议问题