help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Not matching string with wildcard in if statement


From: Jesse Hathaway
Subject: Re: Not matching string with wildcard in if statement
Date: Mon, 9 Mar 2020 11:29:45 -0500

On Mon, Mar 9, 2020 at 11:21 AM Jeffrey Walton <address@hidden> wrote:
>     # Echo variables
>     IOS_SDK: iPhoneOS
>     IOS_CPU: armv7s
>     # set -x
>     +++[[ iPhoneOS == \i\P\h\o\n\e\O\S ]]
>     +++[[ armv7s == \a\r\m\v\7\* ]]
>     +++[[ iPhoneOS == \i\P\h\o\n\e\O\S ]]
>     +++[[ armv7s == \a\r\m\6\4 ]]
>     ...
>
> Why am I failing to match the string "armv7s" with "armv7*" ?

If you quote the right hand side of a `==` test then you test for
string equality.

If you want to test against a glob then the right hand side needs to
be unquoted,
another option is to use a regex test `=~`, see below:

#!/bin/bash

IOS_CPU=armv7s
IOS_SDK=iPhoneOS

if [[ "$IOS_SDK" == "iPhoneOS" && "$IOS_CPU" == "armv7*" ]]; then
  printf 'Matched arm with string equality\n'
fi

if [[ "$IOS_SDK" == "iPhoneOS" && "$IOS_CPU" == armv7* ]]; then
  printf 'Matched arm with glob\n'
fi

if [[ "$IOS_SDK" == "iPhoneOS" && "$IOS_CPU" =~ armv7.* ]]; then
  printf 'Matched arm with regex\n'
fi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]