19

I realise that shift moves the array of cli args n space to the left, and the default of n is 1. This means I can assign the values the array to existing varibles using $1 shift inside a while loop. What I don't quite understand is why it is used in this context below. The input args have been assigned to values already and deleting shift $((OPTIND-1)) doesnt change this fact. Source: http://linux.die.net/man/3/optind

while getopts ":h:a:fc" opt; do
    case $opt in
        h)
            print_help
            exit 0
            ;;
        a)
            aaaa=${OPTARG}
            ;;
        f)
            force=1
            ;;
        c)
            CLEAN=1
            ;;
        \?)
            echoerr "Invalid option -$OPTARG"
            print_help
            exit 1
            ;;
    esac
done

shift $((OPTIND-1))
1
  • 2
    What if there are also positional parameters? Oct 10, 2014 at 7:41

2 Answers 2

42

The shift removes the parameters processed by the getopts loop from the parameter list, so that the rest of the script can process the remainder of the command line (if any) with $1... in the usual manner, without concern for the number of options processed by getopts.

Consider a hypothetical script with the usage

frobble [-f] [-c] [-a hostname] filename ...

The getopts loop above takes care of parsing the -f,-c and -a, if they are present, but doesn't remove them from the argument list. Which means that to get at your filename argument, you need to find out how many options were processed, and continue processing from there. Conveniently, getopts tells you the index of the first unprocessed argument: the variable OPTIND.

And instead of messing with offsets and stuff, you can just discard the processed options, renumbering the rest of the arguments so your filename is always $1.

That is what shift $((OPTIND-1)) does.

2
  • 3
    The shift syntax is overlooked here. shift [n] is a bash/sh builtin command that will shift the argument index by n. Thus, if your command has, for instance, 3 arguments, arg1, arg2, and arg3, $1 will correspond to arg1, $2 will correspond to arg2, etc. If you call shift 2 in your script, $1 will now correspond to arg3.
    – nucatus
    Jun 30, 2022 at 8:44
  • @nucatus Not overlooked. As the poster stated in the very first sentence of the question that they understood the syntax of shift, I saw no reason to re-explain the obvious. But if you only read the answer, it may be useful to have it restated, so thanks for the clarification. Jul 26, 2022 at 12:28
0
  1. The expression is normally found immediately after a getopts while loop. $OPTIND is the number of options found by getopts.

  2. OPTIND gives the position of the next command line argument.

  3. shift pops the first argument from the list of arguments.

  4. shift N pops the first N arguments from the list of arguments.

However, this short expression contains a syntax error. This error has propagated throughout the interwebs. The correct syntax is:

shift "$((OPTIND-1))"

Expressions yielding values should be double-quoted, to guard against results containing embedded spaces

1
  • 1
    There is no need to double-quote the result of this arithmetic expansion. Also, the getops command declares OPTIND variable with the integer attribute set (declare -i OPTIND), which cannot contain embedded spaces as it is evaluated even if the $((..)) expansion is not used. Statement OPTIND="str ing" will throw a syntax error and OPTIND value will remain the same. (If the expression is invalid, Bash prints a message indicating failure to the standard error and no substitution occurs.) gnu.org/software/bash/manual/html_node/…
    – Andi
    Jan 3 at 13:55

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.