I can see in unix shell scripts that following construction used
[ x"$VAR" = x"VALUE" ]
instead of
[ "$VAR" = "VALUE" ]
Why?
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.
Because shells were not always entirely well-behaved if one of the variables was empty.
Consider if
$VARwas empty/null and$VALUEis “foo”, your two constructs expand to:and
The latter would cause an error in some shells due to being an illegal construct, while the former is valid in any case. This is not a problem in recent version of bash (perhaps even old versions of bash) but that’s where it comes from historically – and asides from the occasional moment of puzzlement from people in your situation, there’s little reason not to do it for compatibility with a wider range of shells.