Test: Difference between revisions
Created page with "The test command (or <code>[[ ]]</code>) is used to test various conditions, or to compare strings. When comparing strings in Bash you can use the following operators: <cod..." |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
When comparing strings in Bash you can use the following operators: | When comparing strings in Bash you can use the following operators: | ||
<b>Equals/Not Equals:</b> | |||
<code>string1 = string2</code> and <code>string1 == string2</code> - The equality operator returns true if the operands are equal. | <code>string1 = string2</code> and <code>string1 == string2</code> - The equality operator returns true if the operands are equal. | ||
- Use the = operator with the test [ command.<br /> | - Use the = operator with the test [ command.<br /> | ||
- Use the == operator with the [[ command | - Use the == operator with the [[ command.. | ||
string1 != string2 - The inequality operator returns true if the operands are not equal. | |||
<b>Regex Match:</b> | |||
string1 =~ regex- The regex operator returns true if the left operand matches the extended regular expression on the right. | |||
<b>Greater than/Less than:</b> | |||
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. | |||
<b>String Length:</b> | |||
-z string - True if the string length is zero.<br /> | |||
-n string - True if the string length is non-zero. | |||
<b>Note:</b> | |||
- A blank space must be used between the binary operator and the operands.<br /> | |||
- Always use double quotes around the variable names to avoid any word splitting or globbing issues. |
Latest revision as of 18:53, 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:
Equals/Not Equals:
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..
string1 != string2 - The inequality operator returns true if the operands are not equal.
Regex Match:
string1 =~ regex- The regex operator returns true if the left operand matches the extended regular expression on the right.
Greater than/Less than:
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.
String Length:
-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.