
The shift keyword takes an additional argument of how many positions of the arguments to move the cursor to. This is where the shift keyword is helpful. Now, this output can be treated as positional arguments, and loops are written to iterate through each of them.

Notice that, the utility has added a trailing double hyphen ( -) denoting the end of the output. Here's a visual representation of the output generated by the script and a demonstration of the QA environments: Upon passing arguments to this script, it results in the following output: $ bash test_options.sh -city1 Paris -city2 NewYork OPTS=$(getopt -a -n weather -options $SHORT -longoptions $LONG - " ") The getopt utility puts the input arguments to an organized positional output.įor example, let's tweak test_options.sh to print our options: #!/bin/bash In the code above, the arguments c and d (followed by a colon) require the value to be sent, while the option h doesn't require any arguments (no colon).

The command-line utility getopt solves this problem by providing syntax and options to define and parse the arguments. In such cases, a solid framework needs to be in place. This method doesn't hold well if there's an increase in the number of arguments or there's a conditional requirement for the assignment of the variables. In the previous section, you understood how the positional arguments are passed into the script. This shell script is executed with the arguments as shown below: $ bash arg_intro.sh runtime inputs #!/bin/bash echo "Script name is: $0" echo "Arg1 is $1" echo "Arg1 is $2" echo "Arg1 is $3" echo "-" echo "All args: $*" echo "All args count: $#"
