How do I compare a variable to a string (and do something if they match)?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using variables in if statements
If you want to do something when they don’t match, replace
=with!=. You can read more about string operations and arithmetic operations in their respective documentation.Why do we use quotes around
$x?You want the quotes around
$x, because if it is empty, your Bash script encounters a syntax error as seen below:Non-standard use of
==operatorNote that Bash allows
==to be used for equality with[, but this is not standard.Use either the first case wherein the quotes around
$xare optional:or use the second case: