So, in the end, if you want to test for string equality, you can use either[ ... ]or[[ ... ]], but you must quote your parameters. If you want to do glob pattern matching, you must leave off the quotes, and use[[ ... ]]. 因此,最后,如果要测试字符串相等性,可以使用[...]...
Output: Test case #1 Test case #2 Test case #3 循环和跳过 For循环允许您执行语句的特定次数。 用数字循环: 在下面的例子中,循环将迭代5次。 #!/bin/bash for i in {1..5} do echo $i done 1. 2. 3. 4. 5. 6. 使用字符串进行循环: 我们也可以循环字符串。 #!/bin/bash for X in cya...
[me@linux ~]$ myString1=abc ; myString2=ab* # Without quotes, Bash will perform glob pattern and fail to test for equality properly [me@linux ~]$ [[ $myString1 == $myString2 ]]; echo $? 0 [me@linux ~]$ [[ $myString1 == "$myString2" ]]; echo $? 1 # Correct string...
=equality, inequality&bitwise AND^bitwise XOR|bitwise OR&&logical AND||logical ORexpr?expr:exprconditional operator=, *=, /=, %=,+=, -=, <<=, >>=,&=, ^=, |=assignment Shell variables are allowed as operands. The name of the variable is replaced by its value (coerced to a fixed...
Example 1: If statement in bash on string equality #!/bin/bash read-p"Enter a match word: "USER_INPUT if[[$USER_INPUT=="hello"]];then echo"Hello to you as well" fi In this example above we use the basicIFcondition with a single test of string equality and then print some output...
See the description of the test builtin command (section SHELL BUILTIN COMMANDS below) for the handling of parameters (i.e. missing parameters). When the == and != operators are used, the string to the right of the operator is consid‐ ...
When not performing substring expansion, using the forms documented below, bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset. ${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of ...
For comparison, you can also use ‘-eq’ for equality, ‘-ne’ for not equality and ‘-gt’ for greater than in bash script. #!/bin/bash n=10 if [ $n -lt 10 ]; then echo "It is a one digit number" else echo "It is a two digit number" fi Run the file with bash ...
=equality, inequality&bitwise AND^bitwise XOR|bitwise OR&&logical AND||logical ORexpr?expr:exprconditional operator=, *=, /=, %=,+=, -=, <<=, >>=,&=, ^=, |=assignment Shell variables are allowed as operands. The name of the variable...
The not equal expression is frequently combined with if or elif expressions to test for equality and execute sentences. -ne only works when brackets surround it [[]]. [[Value1 -ne Value2]] Value1 is generally a bash variable compared to Value2, which is a number. -ne cannot be used...