37

I am trying to see if a string is part of another string in shell script (#!bin/sh).

The code i have now is:

#!/bin/sh
#Test scriptje to test string comparison!

testFoo () {
        t1=$1
        t2=$2
        echo "t1: $t1 t2: $t2"
        if [ $t1 == "*$t2*" ]; then
                echo "$t1 and $t2 are equal"
        fi
}

testFoo "bla1" "bla"

The result I'm looking for, is that I want to know when "bla" exists in "bla1".

Thanks and kind regards,

UPDATE: I've tried both the "contains" function as described here: How do you tell if a string contains another string in Unix shell scripting?

As well as the syntax in String contains in bash

However, they seem to be non compatible with normal shell script (bin/sh)...

Help?

Community
  • 1
  • 1
AlexT82
  • 1,114
  • 1
  • 7
  • 12
  • check this http://stackoverflow.com/questions/229551/string-contains-in-bash – YTech Nov 10 '13 at 15:56
  • Same question and answered here: http://stackoverflow.com/questions/229551/string-contains-in-bash http://stackoverflow.com/questions/2829613/how-do-you-tell-if-a-string-contains-another-string-in-unix-shell-scripting – n2studio Nov 10 '13 at 15:57

1 Answers1

81

When using == or != in bash you can write:

if [[ $t1 == *"$t2"* ]]; then
    echo "$t1 and $t2 are equal"
fi

Note that the asterisks go on the outside of the quotes and that the wildcard pattern must be on the right.

For /bin/sh, the = operator is for equality only, not pattern matching. You can use case for pattern matching though:

case "$t1" in
    *"$t2"*) echo t1 contains t2 ;;
    *) echo t1 does not contain t2 ;;
esac

If you're specifically targeting Linux, I would assume the presence of /bin/bash.

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 1
    Thanks a bunch Glenn! very much appreciated! (and it's a linux environment, but very lightweight, and without /bin/bash so i really needed that /bin/sh version! - thanks again!) – AlexT82 Nov 17 '13 at 11:21
  • Better than coping with case, use =~ operator: if [[ $t1 =~ %t2* ]]; the echo match; fi – lef Oct 03 '14 at 07:40
  • @lef, for a regular expression, you want `[[ $t1 =~ "$t2" ]]` – glenn jackman Oct 03 '14 at 10:30
  • @glenn, right, besides a typo (%t2 instead of $t2), sh actually may not support double brackets [[ ]] – lef Oct 03 '14 at 13:04
  • Omg this is so counter intuitive! Thanks now it makes sense. – Alex Nov 11 '16 at 21:46
  • @alex there is not much that's intuitive about bash – glenn jackman Nov 11 '16 at 21:48
  • @glennjackman Thanks I'm only learning, and I made exactly the same mistake as the author of this question, e.g. put the wildcard between speech marks. – Alex Nov 11 '16 at 22:08
  • @glenn jackman, hey bud, can you help me how to get my script done? I have trouble in getting the `*856*` as `*` is a wildcard. This is my script: `findFile() { while read -r LINE; do printf "$LINE\n"; grep -il '$LINE' *856*; done < /path/to/file.txt }` – WashichawbachaW Oct 12 '17 at 00:35
  • 1
    Notice that order matters: the wilds cards need to be on the right hand side of the comparison... at least from my experience. – barakbd Aug 01 '18 at 18:29