let - Linux


The let command in Linux is used for arithmetic operations. It enables the execution of arithmetic expressions using integers in shell scripts. It is typically used in scripting to perform integer math, especially in conditional statements and loops where mathematical calculations are required.


The basic syntax of the let command is as follows:

let expression

Where expression denotes the arithmetic operation to be performed. Multiple expressions can be provided as arguments separated by spaces:

let expression1 expression2

Each expression should be valid under standard arithmetic rules and Bash’s syntax. The command can handle standard operators like +, -, *, /, and %.


let does not have specific options or flags; it directly takes arithmetic expressions as arguments. The behavior of the command depends solely on the expressions provided.


  1. Basic arithmetic:

    let result=5+3
    echo $result  # Outputs: 8
  2. Using multiple expressions:

    let a=5+3 b=2*3 c=10-3
    echo $a $b $c  # Outputs: 8 6 7
  3. Incrementing a variable:

    let i=i+1
  4. Complex calculation:

    let result=3*(2+3)
    echo $result  # Outputs: 15

Common Issues

  • Syntax Errors: let requires spaces around operators and expressions to parse them correctly. Enclosing complex expressions in quotes can help avoid unexpected syntax errors.


    let "a = 2 * (3 + 4)"
  • Non-integer Arithmetic: let handles only integer arithmetic. Usage with floating points will result in errors or unintended results.

    Use bc for floating-point arithmetic:

    result=$(bc <<< "scale=2; 3.5/2")


let is useful in scripts where looping or conditional execution depends on numeric values. Below is an example script integrating let with other commands:


for ((i = 0; i <= 10; i++)); do
  let square=i*i
  echo "Square of $i is $square"
  • expr: An alternative to let for arithmetic operations which can also handle string operations and comparisons.
  • ((...)): Another shell arithmetic expansion, more versatile and common in scripts.
  • bc: A command-line calculator for more complex arithmetic including floating-point math.

For more comprehensive arithmetic operations and scripting integration, reviewing the official Bash scripting manuals or guides can provide deeper insights.