Erik Sapir Erik Sapir - 4 months ago 9x
Linux Question

How to compare strings in Bash script

I need to compare a variable to some string (and do something if they match).


Try this:

if [ "$x" == "valid" ]; then
  echo "x has the value 'valid'"

If you want to do something when they don't match, replace == with !=. You want the quotes around $x, because if $x is empty, you'll get if [ == "valid" ]... which is a syntax error.