How to check if a string contains a substring in Bash

0 votes

I have a string in Bash:

string="My string"

How can I test if it contains another string?

if [ $string ?? 'foo' ]; then
  echo "It's there!"
fi

Where ?? is my unknown operator. Do I use echo and grep?

if echo "$string" | grep 'foo'; then
  echo "It's there!"
fi

That looks a bit clumsy.

May 27, 2022 in Linux Administration by Rahul
• 3,380 points
1,206 views

1 answer to this question.

0 votes
You ought to recollect that shell prearranging is to a lesser degree a language and to a greater extent an assortment of orders. Instinctually you believe that this "language" expects you to follow an if with a [ or a [[. Both of those are simply orders that return a leave status showing achievement or disappointment (very much like each and every other order). Thus I'd utilize grep, and not the [ order.

Simply do:

in the event that grep - q foo <<<"$string";
    reverberation "It's there"
fi
Now that you are considering if as testing the leave status of the order that follows it (complete with semi-colon), why not reevaluate the wellspring of the string you are trying?

## Rather than this
filetype="$(file - b "$1")"
on the off chance that grep - q "tar document" <<<"$filetype";
#...

## Essentially do this
on the off chance that document - b "$1" | grep - q "tar chronicle";
#...
The - q choice makes grep not yield anything, as we just need the bring code back. <<< causes the shell to extend the following word and use it as the contribution to the order, a one-line variant of the << here record (I'm uncertain about whether this is standard or a Bashism).
answered May 27, 2022 by Korak
• 5,820 points

Related Questions In Linux Administration

0 votes
0 answers

How to split a string into an array in Bash?

In a Bash script, I would like ...READ MORE

Jun 9, 2022 in Linux Administration by Rahul
• 3,380 points
578 views
0 votes
1 answer

How to check permissions of a file in the Linux system?

Hi@akhtar, If you prefer using the command line, ...READ MORE

answered Aug 13, 2020 in Linux Administration by MD
• 95,460 points
1,750 views
0 votes
1 answer

How to check permissions of a folder in the Linux system?

Hi@akhtar, You can use ls command to check ...READ MORE

answered Aug 13, 2020 in Linux Administration by MD
• 95,460 points
9,761 views
0 votes
1 answer

How to find the first field from a file in Bash Shell?

Hi@akhtar, You can extract text from a file. ...READ MORE

answered Oct 20, 2020 in Linux Administration by MD
• 95,460 points
1,081 views
0 votes
1 answer

How to take input from user in bash script?

You can use if-else branch to check ...READ MORE

answered Jan 31, 2019 in Linux Administration by Omkar
• 69,220 points
1,138 views
0 votes
1 answer

How to create a symlink in Linux?

To create a symbolic link, you can ...READ MORE

answered Feb 1, 2019 in Linux Administration by Omkar
• 69,220 points
1,196 views
0 votes
2 answers

how to loop through the content of a file using bash

#!/bin/bash for i in  `cat peptides.txt` do echo $i done READ MORE

answered Sep 5, 2020 in Linux Administration by Prakash K. Aithal
2,986 views
0 votes
1 answer

Comparing two string variables in an 'if' statement in Bash

1. For string comparison, use: if [ "$s1" ...READ MORE

answered May 15, 2019 in Linux Administration by Shubham
• 13,490 points
1,749 views
0 votes
1 answer

How do I check if a directory exists in a Bash shell script?

To check if a directory exists in ...READ MORE

answered Jun 7, 2022 in Linux Administration by Korak
• 5,820 points
1,813 views
0 votes
1 answer

How to keep quotes in Bash arguments?

utilizing "$@" will substitute the contentions as ...READ MORE

answered Jun 13, 2022 in Linux Administration by Korak
• 5,820 points
970 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP