Read options values

We have seen how to identify options and parameters, but we still need a way to read the options values correctly.

You may need to pass a value for a specific option. How can this value be read?

We will check for the $2 variable while the iteration goes through the options that we expect a value for.

Check the following code:

#!/bin/bashwhile [ -n "$1" ]docase "$1" in-a) echo "-a option passed";;-b) param="$2"echo "-b option passed, with value $param"shift ;;-c) echo "-c option passed";;--) shiftbreak ;;*) echo "Option $1 not an option";;esacshiftdonenum=1for param in "$@"doecho "#$num: $param"num=$(( $num + 1 ))done

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.