简体   繁体   中英

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[@]}

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 == .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM