0

The FOR loop has the following syntax:

for v in `cat test.txt`
do
  ...
done

I want to know if there is a syntax like:

if v in  `cat test.txt`
then
   ...
fi

Basically, I want to know if a string is in a variable that contains a list of strings. The bash array should be able to do this. But I want a simpler solution.

William
  • 761
  • 2
  • 10
  • 27
  • Why don’t you use grep? – Amir Afghani Nov 14 '20 at 15:34
  • If you want to know if a string contains something, check this. https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash – ikenas Nov 14 '20 at 15:35
  • Yes, I can use grep or string match, but that seems a little odd. I need to tell if an element is in a list, not match a string against a line or the whole variable. – William Nov 14 '20 at 15:46
  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus Nov 14 '20 at 15:52
  • How did you define your variable? The best way to do this is to make it an associative array, with the strings you want to search for as keys. – Charles Duffy Nov 14 '20 at 16:31

0 Answers0