code_junkie

String contains in bash

Nov 11th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. string=`echo My string`
  2.  
  3. if [ $string ?? 'foo' ] then;
  4. echo "It's there!";
  5. fi;
  6.  
  7. if [ `echo $string || grep 'foo' ` ] then;
  8. echo "It's there!";
  9. fi;
  10.  
  11. string='My string';
  12.  
  13. if [[ $string == *My* ]]
  14. then
  15. echo "It's there!";
  16. fi
  17.  
  18. case "$string" in
  19. *foo*)
  20. # Do stuff
  21. ;;
  22. esac
  23.  
  24. string='My string';
  25.  
  26. if [[ $string =~ .*My.* ]]
  27. then
  28. echo "It's there!"
  29. fi
  30.  
  31. if [ "$string" != "${string/foo/}" ]; then
  32. echo "It's there!"
  33. fi
  34.  
  35. if grep -q foo <<<$string; then
  36. echo "It's there"
  37. fi
  38.  
  39. text=" <tag>bmnmn</tag> "
  40. if [[ "$text" =~ "<tag>" ]]; then
  41. echo "matched"
  42. else
  43. echo "not matched"
  44. fi
  45.  
  46. str=this_is_my_string
  47. if [ `expr match "$str" ".*my.*"` != "0" ]; then
  48. echo "there";
  49. fi;
  50.  
  51. expr "$variable" : 'regex'
  52.  
  53. if [ "$string" != "${string/foo/}" ]; then echo "It's there!" fi
  54.  
  55. [ $(expr $mystring : ".*${search}.*") -ne 0 ] && echo 'yes' || echo 'no'
  56.  
  57. [ $(expr $mystring : ".*${search}.*") -ne 0 ] && echo 'yes' || echo 'no'
Add Comment
Please, Sign In to add comment