Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - Check if string is in list [duplicate]

Tags:

linux

bash

shell

I have a bash script and I want to check if a string is in a list. Like: string = "Hello World!", List=("foo", "bar"). Python example:

if name in list: # Another way -> if name in ["foo", "bar"]
  # work to do
else:
  sys.exit(1)

Thank you!

like image 615
schui Avatar asked Sep 16 '25 02:09

schui


1 Answers

There are a number of ways, the simplest I see is:

#!/bin/sh

WORD_LIST="one two three"
MATCH="twox"

if echo "$WORD_LIST" | grep -qw "$MATCH"; then
    echo "found"
else
    echo "not found"
    exit 1
fi
like image 70
Marco Silva Avatar answered Sep 17 '25 14:09

Marco Silva