Boozle
Boozle

Reputation: 21

How to match a hyphen in bash

Part of my script takes in all parameters and looks for any flag options. I'm trying to save these into my array, but it doesn't seem to be matching. I can't figure it out, what am I missing?

#!/bin/bash
ALL_PARAMS=( "$@" )

ARGUMENTS=()

OPTIONS=()

for i in ${ALL_PARAMS[@]}

do

    if [ $i == ^- ]
    then
        ARGUMENTS+=($i)
    else
        OPTIONS+=($i)
    fi
done

echo ${ARGUMENTS[@]}
echo ${OPTIONS[@]}

Upvotes: 1

Views: 1724

Answers (1)

heemayl
heemayl

Reputation: 42007

The test command ([) does not do Regex matching, the bash keyword [[ does.

You need:

[[ $i =~ ^- ]]

Also note that, you need Regex operator =~ instead of equality operator ==.

Upvotes: 2

Related Questions