0

I'm using the getopts command to process the flags in my bash script, using the structure found in this answer: https://stackoverflow.com/a/21128172/2230446

#!/bin/bash
t_flag='30'

print_usage() {
  printf "Usage: ..."
}

while getopts 't:' flag; do
  case "${flag}" in
    t) t_flag="${OPTARG}" ;;
    *) print_usage
       exit 1 ;;
  esac
done

echo "${t_flag}"

I run the script with the following command:

./test.sh -t dog cat fish

My goal is to extract cat fish to a variable so I can use it in the rest of the script, similar to how the t_flag was extracted to a variable.

Alex Lamson
  • 479
  • 5
  • 14

1 Answers1

1

Discard -t dog after parsing it, e.g:

shift $((OPTIND-1))

Then only cat and fish will be left as positional parameters. To extract them to a variable:

var=$*
oguz ismail
  • 1
  • 16
  • 47
  • 69