Shell test ([]) compares the use of string X "$variable"

Source: Internet
Author: User
Tags comparison
use of string comparison X "$variable" in Shell

This article refers to StackOverflow why does shell script comparisons often use X$var = Xyes? do not use double quotes "", do not use prefix x do not use double quotes "", use prefix x use double quotation marks "" without using prefix x use the double quotation mark "", using the prefix x

Shell string comparison xvariable using double quotation marks without prefix x using double quotes without prefix x using double quotation marks with prefix x using double quotation marks X

do not use double quotes "", do not use prefix x

Variable Shell_var undefined ($SHELL _var is empty)
if test $SHELL _var = yes; Then--and if test = yes; Then
Obviously there is a syntax error, and the test loses the parameter. do not use double quotes "", use prefix x

$SHELL _var value is empty
if test X$shell_var = yes; Then--and if test x = yes; Then
It seems that there is no problem in looking at it.
However, if the $shell_var value is "yes" at this point, note that there is a space in front of Yes, then:
if test X$shell_var = yes; Then--and if test x Yes = yes; Then
Obviously this is also a syntax error, test has two parameters X and 1. use double quotation marks "" without using prefix x

$SHELL _var value is empty
if test "$SHELL _var" = "yes"; Then--and if test "=" yes "; Then
$SHELL _var value is "yes"
if test "$SHELL _var" = "yes"; Then--and if test "yes" = "yes"; Then
It seems that there is no problem in wrapping up variables with "".
But if the $shell_var value is "-N" or "-F" at this time
if test "$SHELL _var" = "yes"; Then--and if test "-F" = "yes"; Then
Then "-F" is a two semantic, which is either the option of the test command or the argument of test. use the double quotation mark "", using the prefix x

$SHELL _var value is empty
if test X "$SHELL _var" = x "yes"; Then--and if test X "" = x "yes"; Then
$SHELL _var value is "yes"
if test X "$SHELL _var" = x "yes"; Then--and if test X "yes" = x "yes"; Then
$SHELL _var value is "-N" or "-F"
if test X "$SHELL _var" = x "yes"; Then--and if Test X "-F" = x "yes"; Then

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.