Test: Difference between revisions

From Psygen Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 8: Line 8:
- Use the = operator with the test [ command.<br />
- Use the = operator with the test [ command.<br />
- Use the == operator with the [[ command for pattern matching.
- Use the == operator with the [[ command for pattern matching.


string1 != string2 - The inequality operator returns true if the operands are not equal.
string1 != string2 - The inequality operator returns true if the operands are not equal.


string1 =~ regex- The regex operator returns true if the left operand matches the extended regular expression on the right.
string1 =~ regex- The regex operator returns true if the left operand matches the extended regular expression on the right.


string1 > string2 - The greater than operator returns true if the left operand is greater than the right sorted by lexicographical (alphabetical) order.
string1 > string2 - The greater than operator returns true if the left operand is greater than the right sorted by lexicographical (alphabetical) order.


string1 < string2 - The less than operator returns true if the right operand is greater than the right sorted by lexicographical (alphabetical) order.
string1 < string2 - The less than operator returns true if the right operand is greater than the right sorted by lexicographical (alphabetical) order.


-z string - True if the string length is zero.
-z string - True if the string length is zero.

Revision as of 18:49, 6 May 2019

The test command (or [[ ]]) is used to test various conditions, or to compare strings.


When comparing strings in Bash you can use the following operators:

string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal.

- Use the = operator with the test [ command.
- Use the == operator with the [[ command for pattern matching.


string1 != string2 - The inequality operator returns true if the operands are not equal.


string1 =~ regex- The regex operator returns true if the left operand matches the extended regular expression on the right.


string1 > string2 - The greater than operator returns true if the left operand is greater than the right sorted by lexicographical (alphabetical) order.


string1 < string2 - The less than operator returns true if the right operand is greater than the right sorted by lexicographical (alphabetical) order.


-z string - True if the string length is zero. -n string - True if the string length is non-zero.


Note:

- A blank space must be used between the binary operator and the operands.
- Always use double quotes around the variable names to avoid any word splitting or globbing issues.