How to define a shell script with variable number of arguments?

后端 未结 3 552
栀梦
栀梦 2020-12-08 02:22

I would like to define a simple abbreviation of a call to gs (ghostscript) via a shell script. The first argument(s) give all the files that should be merged, t

3条回答
  •  遥遥无期
    2020-12-08 03:12

    To access the last argument, in addition to Idelic's answer above, you can also do:

    echo "${@: $#}"
    

    This reads all of the arguments and prints them starting from the last one. This way, you can also access the N last arguments, for example for the last three arguments:

    echo "${@: $#-2}"
    
    $ ./script "what    does" "this  script" "do" "?"
    this  script do ?
    

提交回复
热议问题