Bash.Arithmetic (Bash v0.6.0)

Copy Markdown View Source

Evaluates bash arithmetic expressions.

Supports the full range of bash arithmetic operators:

  • Arithmetic: + - * / % **
  • Comparison: < > <= >= == !=
  • Logical: && || !
  • Bitwise: & | ^ ~ << >>

  • Assignment: = += -= *= /= %= <<= >>= &= ^= |=
  • Increment/decrement: ++ --
  • Ternary: ?:
  • Parentheses for grouping

Variable references work without $ prefix (e.g., x + 1 where x is a variable).

Examples

iex> Arithmetic.evaluate("1 + 2", %{})
{:ok, 3, %{}}

iex> Arithmetic.evaluate("x + 1", %{"x" => "5"})
{:ok, 6, %{"x" => "5"}}

iex> Arithmetic.evaluate("x = 5", %{})
{:ok, 5, %{"x" => "5"}}

Reference: https://cgit.git.savannah.gnu.org/cgit/bash.git/plain/builtins/let.def?h=bash-5.3