Greg Donald : How to filter out Bash arguments Page

How to filter out Bash arguments

Ever want to know how to drop an argument (and value), --dir in this case, from a Bash script?

Someone from my local LUG asked how to do it and this is what I came up with:

Fun ;)

#!/usr/bin/env bash

args=("$@")
myargs=()
nextarg=-1

for ((i=0; i<$#; i++)) {
   if [ $nextarg == $i ]; then continue; fi
   case ${args[$i]} in
       --dir) nextarg=$((i+1)) ;;
       *) myargs+="${args[$i]} "
   esac
}

echo $myargs
./remove_dir.bash --dir foo --bar baz
--bar baz

bash (3)