Remove last argument from argument list of shell script (bash)

后端 未结 3 755
伪装坚强ぢ
伪装坚强ぢ 2020-12-24 13:47

This question concerns a bash script that is run in automator osx. I am using automator actions to get and filter a bunch of file references from the finder. Then I append t

3条回答
  •  滥情空心
    2020-12-24 14:23

    Assuming that you already have an array, you can say:

    unset "array[${#array[@]}-1]"
    

    For example, if your script contains:

    array=( "$@" )
    unset "array[${#array[@]}-1]"    # Removes last element -- also see: help unset
    for i in "${array[@]}"; do
      echo "$i"
    done
    

    invoking it with: bash scriptname foo bar baz produces:

    foo
    bar
    

提交回复
热议问题