YouTip LogoYouTip

Linux Shell Basic Operators

Shell Basic Operators | Tutorial\n\nShell, like other programming languages, supports multiple operators, including:\n\n* Arithmetic Operators\n* Relational Operators\n* Boolean Operators\n* String Operators\n* File Test Operators\n\nNative bash does not support simple math operations, but it can be achieved through other commands, such as awk and expr, with expr being the most commonly used.\n\nexpr is an expression evaluation tool that can be used to evaluate expressions.\n\nFor example, adding two numbers (**note the use of backticks ` instead of single quotes '**):\n\n## Example\n\n#!/bin/bash\n\nval=`expr 2 + 2`\n\necho"the sum of the two numbers is : $val"\n\n[Run Example Β»](#)\n\nExecute the script, the output is as follows:\n\nthe sum of the two numbers is : 4\nTwo points to note:\n\n* There must be spaces between the expression and operators, for example, 2+2 is incorrect, it must be written as 2 + 2, which is different from most programming languages we are familiar with.\n* The complete expression must be enclosed in ` `, note that this character is not the commonly used single quote, it is located under the Esc key.\n\n* * *\n\n## Arithmetic Operators\n\nThe following table lists commonly used arithmetic operators, assuming variable a is 10, and variable b is 20:\n\n| Operator | Description | Example |\n| --- | --- | --- |\n| + | Addition | `expr $a + $b` results in 30. |\n| - | Subtraction | `expr $a - $b` results in -10. |\n| * | Multiplication | `expr $a * $b` results in 200. |\n| / | Division | `expr $b / $a` results in 2. |\n| % | Modulo | `expr $b % $a` results in 0. |\n| = | Assignment | a=$b assigns the value of variable b to a. |\n| == | Equal. Used to compare two numbers, returns true if they are the same. | [ $a == $b ] returns false. |\n| != | Not equal. Used to compare two numbers, returns true if they are not the same. | [ $a != $b ] returns true. |\n\n**Note:** Conditional expressions must be placed between square brackets and must have spaces, for example: **[$a==$b]** is incorrect, it must be written as **[ $a == $b ]**.\n\n### Example\n\nExamples of arithmetic operators are as follows:\n\n## Example\n\n#!/bin/bash\n\n# author:\n\n# url:example.com\n\na=10\n\nb=20\n\nval=`expr$a + $b`\n\necho"a + b : $val"\n\nval=`expr$a - $b`\n\necho"a - b : $val"\n\nval=`expr$a *$b`\n\necho"a * b : $val"\n\nval=`expr$b/$a`\n\necho"b / a : $val"\n\nval=`expr$b%$a`\n\necho"b % a : $val"\n\nif[$a == $b]\n\nthen\n\necho"a equal to b"\n\nfi\n\nif[$a!= $b]\n\nthen\n\necho"a not equal to b"\n\nfi\n\nExecute the script, the output is as follows:\n\na + b : 30 a - b : -10 a * b : 200 b / a : 2 b % a : 0 a not equal to b\n> **Note:**\n> \n> \n> * The multiplication sign (*) must be preceded by a backslash () to perform multiplication;\n> * if...then...fi is a conditional statement, which will be explained later.\n> * In MAC, the shell's expr syntax is: **$((expression))**, and the "*" in the expression here does not need the escape symbol "".\n\n* * *\n\n## Relational Operators\n\nRelational operators only support numbers and do not support strings, unless the value of the string is a number.\n\nThe following table lists commonly used relational operators, assuming variable a is 10, and variable b is 20:\n\n| Operator | Description | Example |\n| --- | --- | --- |\n| -eq | Checks if two numbers are equal, returns true if they are equal. | [ $a -eq $b ] returns false. |\n| -ne | Checks if two numbers are not equal, returns true if they are not equal. | [ $a -ne $b ] returns true. |\n| -gt | Checks if the number on the left is greater than the one on the right, if so, returns true. | [ $a -gt $b ] returns false. |\n| -lt | Checks if the number on the left is less than the one on the right, if so, returns true. | [ $a -lt $b ] returns true. |\n| -ge | Checks if the number on the left is greater than or equal to the one on the right, if so, returns true. | [ $a -ge $b ] returns false. |\n| -le | Checks if the number on the left is less than or equal to the one on the right, if so, returns true. | [ $a -le $b ] returns true. |\n\n### Example\n\nExamples of relational operators are as follows:\n\n## Example\n\n#!/bin/bash\n\n# author:\n\n# url:example.com\n\na=10\n\nb=20\n\nif[$a-eq$b]\n\nthen\n\necho"$a -eq $b : a equal to b"\n\nelse\n\necho"$a -eq $b: a not equal to b"\n\nfi\n\nif[$a-ne$b]\n\nthen\n\necho"$a -ne $b: a not equal to b"\n\nelse\n\necho"$a -ne $b : a equal to b"\n\nfi\n\nif[$a-gt$b]\n\nthen\n\necho"$a -gt $b: a greater than b"\n\nelse\n\necho"$a -gt $b: a not greater than b"\n\nfi\n\nif[$a-lt$b]\n\nthen\n\necho"$a -lt $b: a less than b"\n\nelse\n\necho"$a -lt $b: a not less than b"\n\nfi\n\nif[$a-ge$b]\n\nthen\n\necho"$a -ge $b: a greater than or equal to b"\n\nelse\n\necho"$a -ge $b: a less than b"\n\nfi\n\nif[$a-le$b]\n\nthen\n\necho"$a -le $b: a less than or equal to b"\n\nelse\n\necho"$a -le $b: a greater than b"\n\nfi\n\nExecute the script, the output is as follows:\n\n10 -eq 20: a not equal to b 10 -ne 20: a not equal to b 10 -gt 20: a not greater than b 10 -lt 20: a less than b 10 -ge 20: a less than b 10 -le 20: a less than or equal to b\n\n* * *\n\n## Boolean Operators\n\nThe following table lists commonly used boolean operators, assuming variable a is 10, and variable b is 20:\n\n| Operator | Description | Example |\n| --- | --- | --- |\n| ! | Negation, returns false if the expression is true, otherwise returns true. | [ ! false ] returns true. |\n| -o | OR operation, returns true if at least one expression is true. | [ $a -lt 20 -o $b -gt 100 ] returns true. |\n| -a | AND operation, returns true only if both expressions are true. | [ $a -lt 20 -a $b -gt 100 ] returns false. |\n\n### Example\n\nExamples of boolean operators are as follows:\n\n## Example\n\n#!/bin/bash\n\n# author:\n\n# url:example.com\n\na=10\n\nb=20\n\nif[$a!= $b]\n\nthen\n\necho"$a != $b : a not equal to b"\n\nelse\n\necho"$a == $b: a equal to b"\n\nfi\n\nif[$a-lt 100-a$b-gt 15]\n\nthen\n\necho"$a less than 100 and $b greater than 15 : returns true"\n\nelse\n\necho"$a less than 100 and $b greater than 15 : returns false"\n\nfi\n\nif[$a-lt 100-o$b-gt 100]\n\nthen\n\necho"$a less than 100 or $b greater than 100 : returns true"\n\nelse\n\necho"$a less than 100 or $b greater than 100 : returns false"\n\nfi\n\nif[$a-lt 5-o$b-gt 100]\n\nthen\n\necho"$a less than 5 or $b greater than 100 : returns true"\n\nelse\n\necho"$a less than 5 or $b greater than 100 : returns false"\n\nfi\n\nExecute the script, the output is as follows:\n\n10 != 20 : a not equal to b 10 less than 100 and 20 greater than 15 : returns true10 less than 100 or 20 greater than 100 : returns true10 less than 5 or 20 greater than 100 : returns false\n\n* * *\n\n## Logical Operators\n\nThe following introduces Shell's logical operators, assuming variable a is 10, and variable b is 20:\n\n| Operator | Description | Example |\n| --- | --- | --- |\n| && | Logical AND | [[ $a -lt 100 && $b -gt 100 ]] returns false |\n| || | Logical OR | [[ $a -lt 100 || $b -gt 100 ]] returns true |\n\n### Example\n\nExamples of logical operators are as follows:\n\n## Example
← Docker Compose Up CommandKubernetes Scaling and Updates β†’