-1

In a shell-script I have a loop over the positional parameters using the shift-command. After the loop I d like to reset and start another loop over the parameters. Is it possible to go back to start?

while [ $# -gt 0 ]; do
  case "$1" in
    "--bla")
      doing sth
      shift 2
      ;;
    *)
      shift 1
      ;;
  esac
done
Cyrus
  • 84,225
  • 14
  • 89
  • 153
steff123
  • 441
  • 1
  • 4
  • 10

2 Answers2

2

You can save arguments in a temporary array. Then restore positional arguments from it.

args=("$@")  # save

while .....
done

set -- "${args[@]}"  # restore
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
1

Don't use shift if you need to process the arguments twice. Use a for loop, twice:

for arg in "$@"
do
    …
done

If you need to process argument options, consider using the GNU version of getopt (rather than the Bash built-in getopts because that only handles short options). See Using getopts in bash shell script to get long and short command line options for many details on how to do that.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278