bash argument case for args in $@

后端 未结 5 1691
无人共我
无人共我 2021-02-02 12:14

I have a script with a long list of OPTIONAL arguments. some have associated values.

Such as:

.script --first 2012-12-25 --last 2012-12-26 --copy --remov         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 12:41

    $@ is an array, & not a simple variable.

    You can capture it to a local variable as x=("$@") & then use array x with indices as 0 to ($# - 1).

    To access individual elements, use ${x[$i]}. You can NOT directly use ${@[$i]}, however.

    So instead of for arg in "$@" loop, you will have i=0; while [ $i -lt $# ]; do loop.

提交回复
热议问题