Linux command
let 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Arithmetic assignment
let "x = 5 + 3"
Increment variable
let "count++"
Multiple expressions
let "a = 1" "b = 2" "c = a + b"
Comparison (exit code)
let "5 > 3"
Modulo operation
let "result = 10 % 3"
说明
let is a Bash (and ksh) built-in that evaluates one or more arithmetic expressions. Each expression is evaluated using the same rules as `$(( ... ))`: integer math, C-style operators, and shell variable references without the leading `$`. The exit status is 0 if the value of the last evaluated expression is non-zero, and 1 if it is zero. This makes `let` usable in `if`/`while` conditions but is the inverse of typical command exit semantics — a successful arithmetic result of 0 (e.g., `let "x = 0"`) reports failure.
FAQ
What is the let command used for?
let is a Bash (and ksh) built-in that evaluates one or more arithmetic expressions. Each expression is evaluated using the same rules as `$(( ... ))`: integer math, C-style operators, and shell variable references without the leading `$`. The exit status is 0 if the value of the last evaluated expression is non-zero, and 1 if it is zero. This makes `let` usable in `if`/`while` conditions but is the inverse of typical command exit semantics — a successful arithmetic result of 0 (e.g., `let "x = 0"`) reports failure.
How do I run a basic let example?
Run `let "x = 5 + 3"` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
Where can I find more let examples?
This page includes 5 examples for let, plus related commands for nearby Linux tasks.