# Substring check in IF condition in shell script

**URL:** <https://community.unix.com/t/substring-check-in-if-condition-in-shell-script/327213>\
**Category:** Shell Programming and Scripting\
**Created:** [April 17, 2013, 9:55am UTC](https://community.unix.com/t/substring-check-in-if-condition-in-shell-script/327213 "2013-04-17T09:55:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greenworld123](https://community.unix.com/letter_avatar/greenworld123/32/6_22734026db12803ebb3e059622ac3534.png) [@greenworld123](https://community.unix.com/u/greenworld123)\
**Post date:** [April 17, 2013, 9:55am UTC](https://community.unix.com/t/substring-check-in-if-condition-in-shell-script/327213/1 "2013-04-17T09:55:19Z")

</div>

I want to check if the string has the substring in IF condition then process...

i tried below but not working

```nohighlight
if [["$field" = reserved* || "$field" = fillspaces*]];
then
.............

```

field can be "reserved1" ....reservedn / fillspaces1 ... fillspacesn

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [April 17, 2013, 10:06am UTC](https://community.unix.com/t/substring-check-in-if-condition-in-shell-script/327213/2 "2013-04-17T10:06:50Z")

</div>

In bash you can use the built-in regexp comparison operator `=~`

```nohighlight
if [["$field" =~ reserved.*]] || [["$field" =~ fillspaces.*]]
then
        ...
fi

```

---

<div class="post-metadata">

**Author:** ![greenworld123](https://community.unix.com/letter_avatar/greenworld123/32/6_22734026db12803ebb3e059622ac3534.png) [@greenworld123](https://community.unix.com/u/greenworld123)\
**Post date:** [April 17, 2013, 10:08am UTC](https://community.unix.com/t/substring-check-in-if-condition-in-shell-script/327213/3 "2013-04-17T10:08:07Z")

</div>

i am using ksh .... "=~" is not working

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [April 17, 2013, 10:47am UTC](https://community.unix.com/t/substring-check-in-if-condition-in-shell-script/327213/4 "2013-04-17T10:47:41Z")

</div>

How about using `grep` command?

```nohighlight
if echo "$field" | grep -q "reserved.*"
then
      ...
fi

```

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [April 17, 2013, 11:56am UTC](https://community.unix.com/t/substring-check-in-if-condition-in-shell-script/327213/5 "2013-04-17T11:56:37Z")

</div>

Builtin solution is to use case in every shell, if [[ ... regexp is not usable.

```nohighlight
case "$field" in
     reserverd*|fillspaces* ) some 
                   ;;
esac

```

ksh93 and bash you can use [[]] testing with patterns. Condition Test.

It's not same as [ / test cmd.

```nohighlight
a=teststring

if [[$a = tes*]] ; then
        echo y1
fi
if [[$a = tes*r*]] ; then
        echo y2
fi
if [[$a = *g]] ; then
        echo y3
fi
if [[$a = *g*1]] ; then # false, no echo
        echo y4
fi

```
