0

What is the equivalent of the python statement in bash:

if "mystr" in "a long string with mystring in it":
    print("found string")

The offset in number of characters can NOT be relied on to be constant, so I don't think I can get cut to work.

2

Use wildcard matching. For bash specifically:

if [[ $longstring == *mystr* ]]; then

if [[ $longstring == *"my string"* ]]; then

if [[ $longstring == *"$matchstr"* ]]; then

In the [[ operator, the left-side is expanded literally (that is, without the need to double-quote), while the unquoted parts of the right-side are treated like wildcards – basically the same quoting rules as in a case statement.

It's possible to use POSIX regex match as well:

if [[ $longstring =~ mystr ]]; then

if [[ $longstring =~ "my string" ]]; then

if [[ $longstring =~ "$matchstr" ]]; then

Again, the quoted parts of the right-side are matched literally while unquoted parts are matched as regex operators.


If you require sh compatibility:

case $longstring in
    *mystr*)
        echo yes;;
    *"$matchstr"*)
        echo yes again!;;
    *)
        echo no;;
esac

Alternatively, if you're into abusing the language:

if case $longstring in
    *mystr*) true;;
    *"$matchstr"*) true;;
    *) false;;
esac; then
    ...
fi

(This if/case/esac/then form is actually useful in some situations, like when making an incremental whitelist/blacklist.)

Shorter version:

if ! case $longstring in *"$matchstr"*) !; esac; then
    echo "It matched!"
fi

Many sh scripts use expr but it's not a shell built-in, therefore a bit slower:

if expr match "$longstring" ".*mystr"; then
    ...
fi

In this case (as expr cannot see differences in quoting) the entire "match" parameter is interpreted as an anchored regex (like grep but always with a ^ prepended). Therefore be careful when passing a variable as the match.

1

This will do it:

#! /bin/bash

X=`echo "a long string with mystring in it" | grep "mystr" | wc -l`
if [ $X -gt 0 ]
then
        echo "found string";
fi

The idea here being that "echo" sets up the string, grep looks for the substring in the string passed to it, and wc -l returns the number of lines the string was found in - so either 0 or 1.

  • found this as well: askubuntu.com/questions/299710/… – tarabyte Dec 30 '15 at 5:21
  • I was just working on something like that for a more sophisticated answer - I have now come up with the following 1 liner (which is arguably closer to what you wanted) : echo "a long string with mystring in it" | grep -q "mystr" && echo "found string" - but your way is more efficient. – davidgo Dec 30 '15 at 5:22
  • 1
    ahhhh! grep returns 0 if match found. much more readable – tarabyte Dec 30 '15 at 5:23
  • 1
    Yes, that means you can simplify it to if echo ... | grep -q ...; then – grawity Dec 30 '15 at 5:52
0

You can use bash parameter expansion:

if [ "${TestString/$SearchString/}" == "$TestString" ]; then echo Found; fi

or if you want a Bourne-compatible expression:

if [ "${TestString#*$SearchString}" == "$TestString" ]; then echo Found; fi

$SearchString can be a literal: it can contain spaces, whether a literal or variable, and a variable can contain }, but in a literal it must be escaped as \}. In the first form, the same consideration also applies to /.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.