ssk
ssk

Reputation: 9275

How to read string after an option on bash?

I am trying to extract the string passed after an option:

eg.

./install -s service1 service2

if [ -z ${1} ] ; then
    echo "No option provided, defaulting to (-h)elp."
    echo
    option="-h"
else
    option=$1

fi

I would like to extract "service1 service2" from the argument list. How to accomplish this?

Upvotes: 2

Views: 241

Answers (1)

l'L'l
l'L'l

Reputation: 47302

You could use shift and the special variable $@:

option=$1
shift
echo "$@"

Returns:

service1 service2

As mentioned, if available getopts is a good option.

Upvotes: 1

Related Questions