How do I compare a variable to a string (and do something if they match)?
if [ "$x" = "valid" ]; then echo "x has the value 'valid'" fi
You want the quotes around
$x, because if it is empty, your bash script encounters a syntax error as seen below:
if [ = "valid" ]; then
== to be used for equality with
[, but this is not standard.
Use either the first case wherein the quotes around
$x are optional:
if [[ "$x" == "valid" ]]; then
or use the second case:
if [ "$x" = "valid" ]; then
Or, if you don't need else clause:
[ "$x" == "valid" ] && echo "x has the value 'valid'"