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:
Syntax
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
#!/bin/bash
let x=5+3
echo "x = $x"
let "y = x * 2"
echo "y = $y"
Login to try C/C++/Java/PHP code in the editor
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
- Putting spaces around
=when usinglet, as inlet x = 5, which fails unless the expression is quoted. - Expecting
letto work with decimals, when it is integer only. - Using
letand forgetting that a result of 0 gives exit status 1, which can stop a script withset -e.
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: