Question

Below is the snippet of my shell script

#!/bin/sh -e
ROOT=""
while getopts "r" optionName;
do case $optionName in
r)
    ROOT="$OPTARG"
    ;;
*)
    echo "Bad argument"
    exit
    ;;
esac
done
echo "ROOT=$ROOT"

it doesn't print the ROOT correctly, where is the problem?

$myscript.sh -r FOO
ROOT=
Was it helpful?

Solution

You need a colon after r if you want it to take an argument:

while getopts "r:" optionName
                ^
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top