I am not a Linux guy but stuck in some Script which I have to read for my Project. So can anyone can help me what this command is doing?
shift $(($optind - 1))
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
shift $((OPTIND-1)) (note
OPTIND is upper case) is normally found immediately after a
$OPTIND is the number of options found by
As pauljohn32 mentions in the comments, strictly speaking,
OPTIND gives the position of the next command line argument.
From the GNU Bash Reference Manual:
getopts optstring name [args]
getoptsis used by shell scripts to parse positional parameters.
optstringcontains the option characters to be recognized; if a character is followed by a colon, the option is expected to have an argument, which should be separated from it by whitespace. The colon (‘:’) and question mark (‘?’) may not be used as option characters. Each time it is invoked,
getoptsplaces the next option in the shell variable name, initializing
nameif it does not exist, and the index of the next argument to be processed into the variable
OPTINDis initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, getopts places that argument into the variable
OPTARG. The shell does not reset
OPTINDautomatically; it must be manually reset between multiple calls to
getoptswithin the same shell invocation if a new set of parameters is to be used.
When the end of options is encountered,
getoptsexits with a return value greater than zero.
OPTINDis set to the index of the first non-option argument, and name is set to ‘?’.
getoptsnormally parses the positional parameters, but if more arguments are given in
getoptsparses those instead.
removes n strings from the positional parameters list. Thus
shift $((OPTIND-1)) removes all the options that have been parsed by
getopts from the parameters list, and so after that point,
$1 will refer to the first non-option argument passed to the script.
As mikeserv mentions in the comment,
shift $((OPTIND-1)) can be unsafe. To prevent unwanted word-splitting etc, all parameter expansions should be double-quoted. So the safe form for the command is
$((...)) just calculates stuff. In your case it takes the value of
$optint and substracts 1.
shift removes positional parameters. In your case it removes
For more information have a look at
help shift, look at
man bash for "Arithmetic Expansion", and especially google for