← Back to Bash Course | Chapter 4: Loops | Lesson 5 of 14

let

let is like telling a helper 'work this out and write the answer on my label'. You give it a small sum and it stores the result in a variable.

In this page:

  1. let
Syntax
bash
let variable=expression

let

let evaluates and assigns the result of an arithmetic expression to a variable, similar to $(( )) but as a standalone command. let x=5+3 sets x to 8 without needing $ on the right-hand side.

It's an older alternative that's less commonly used today than $(( )) or ((...)).

Note: ((x = 5 + 3)) is the modern equivalent of let x=5+3 and is generally preferred.

Example: let

bash
#!/bin/bash
let x=5+3
echo "x = $x"

let "y = x * 2"
echo "y = $y"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Putting spaces around = when using let, as in let x = 5, which fails unless the expression is quoted.
  2. Expecting let to work with decimals, when it is integer only.
  3. Using let and forgetting that a result of 0 gives exit status 1, which can stop a script with set -e.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.