MATH(1)

๐Ÿ’– MATH(1) ๐Ÿ’–

ok so bestie, welcome to the fish-shell manual โ€” this is MATH(1), the math page, and it's literally about to make numbers your whole personality. ๐Ÿงฎโœจ


๐ŸŒธ NAME ๐ŸŒธ

math โ€” perform mathematics calculations. that's it, that's the vibe. ๐Ÿ’…


โœจ SYNOPSIS โœจ

math [(-s | --scale) N] [(-b | --base) BASE] [(-m | --scale-mode) MODE] EXPRESSION ...

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

okay so math performs mathematical calculations. it does the simple stuff โ€” addition, subtraction, and so on โ€” plus the glow-up stuff like functions: abs(), sqrt() and ln(). she's versatile, she contains multitudes. ๐Ÿ’œ

by default, the output shows up to 6 decimal places. want a different number of decimal places? use the --scale option, and yes, --scale=0 is right there for integer output. ๐ŸŽ€

โš ๏ธ big one, don't skip: parameter expansion happens before expressions are evaluated. this is genuinely so useful for doing calculations with shell variables or the output of command substitutions โ€” but it also means parenthesis () and the asterisk * glob character have to be escaped or quoted. also, x can be used to denote multiplication, but it needs to be followed by whitespace so it doesn't get confused with hexadecimal numbers. iykyk ๐Ÿ˜ญ

parentheses for functions are optional โ€” math sin pi prints 0. cute, right? BUT. a comma will bind to the inner function, so math pow sin 3, 5 is an error, because it tries to give sin the arguments 3 and 5. so when in doubt: use parentheses. always. ๐Ÿซถ

math ignores whitespace between arguments and takes its input as multiple arguments (internally joined with a space), so math 2 +2 and math "2 + 2" work the exact same. but math 2 2 is an error. no cap. ๐Ÿ’€

Note: ๐Ÿงš math is a simple tool for simple uses, provided for convenience. it internally works with floats for simplicity of implementation and can't do a number of things real calculators can. if you find yourself limited by that, use a more full-featured tool. she knows her limits, and honestly? respect. ๐Ÿ’…

the following options are available:

๐ŸŽ€ OPTIONS ๐ŸŽ€

  • -s N or --scale N โ€” sets the scale of the result. N must be an integer or the word "max" for the maximum scale. a scale of zero causes results to be truncated by default โ€” any non-integer component is thrown away. so 3/2 returns 1 by default, rather than 2 which 1.5 would normally round to. this is for compatibility with bc, which was the basis for this command prior to fish 3.0.0. scale values greater than zero cause the result to be rounded using the usual rules to the specified number of decimal places. โญ
  • -b BASE or --base BASE โ€” sets the numeric base used for output (math always understands hexadecimal numbers as input, she's just built different). it currently understands "hex" or "16" for hexadecimal and "octal" or "8" for octal, and it implies a scale of 0 (other scales cause an error), so it will truncate the result down to an integer. this might change in the future ๐Ÿ‘€. hex numbers will be printed with a 0x prefix. octal numbers will have a prefix of 0 but aren't understood by math as input. ๐Ÿ™ˆ
  • -m MODE or --scale-mode MODE โ€” sets scale behavior. the MODE can be truncate, round, floor, ceiling. the default value of scale mode is round with non zero scale and truncate with zero scale. ๐Ÿฆ‹
  • -h or --help โ€” displays help about using this command. classic. ๐Ÿ’–

๐Ÿ”ฅ RETURN VALUES ๐Ÿ”ฅ

if the expression is successfully evaluated and doesn't over/underflow or return NaN, the return status is zero (success) โ€” else one. that's the whole mood. โœ…โŒ


๐ŸŒˆ SYNTAX ๐ŸŒˆ

math knows some operators, constants, functions, and can (obviously) read numbers. she's well-rounded. ๐Ÿ‘‘

for numbers, . is always the radix character regardless of locale โ€” 2.5, not 2,5. scientific notation (10e5) and hexadecimal (0xFF) are also available. ๐ŸŒธ

math also lets you use underscores as visual separators for digit grouping. for example, you can write 1_000_000, 0x_89_AB_CD_EF, and 1.234_567_e89. aesthetic and functional, we love to see it. โœจ


๐Ÿ’… OPERATORS ๐Ÿ’…

math knows the following operators:

  • + โ€” for addition
  • - โ€” for subtraction
  • * or x โ€” for multiplication. * is the glob character and needs to be quoted or escaped; x needs to be followed by whitespace or it looks like 0x hexadecimal notation. โš ๏ธ
  • / โ€” for division
  • ^ โ€” for exponentiation
  • % โ€” for modulo
  • ( or ) โ€” for grouping. these need to be quoted or escaped because () denotes a command substitution. ๐Ÿ˜ญ

they are all used in an infix manner โ€” 5 + 2, not + 5 2. we're not doing prefix today bestie. ๐Ÿ™…โ€โ™€๏ธ


๐Ÿงš CONSTANTS ๐Ÿงš

math knows the following constants:

  • e โ€” Euler's number
  • pi โ€” ฯ€, you know this one. half of Tau. ๐ŸŒ—
  • tau โ€” equivalent to 2ฯ€, or the number of radians in a circle

use them without a leading $ โ€” pi - 3 should be about 0. don't put a dollar sign on her, she's not a variable. ๐Ÿ’€


โญ FUNCTIONS โญ

math supports the following functions:

  • abs โ€” the absolute value, with positive sign
  • acos โ€” arc cosine
  • asin โ€” arc sine
  • atan โ€” arc tangent
  • atan2 โ€” arc tangent of two variables
  • bitand, bitor and bitxor โ€” perform bitwise operations. these will throw away any non-integer parts and interpret the rest as an int.
Note: ๐Ÿ’€ bitnot and bitnand don't exist. this is because numbers in math don't really have a width in terms of bits, and these operations necessarily care about leading zeroes.

if you need to negate a specific number you can do it with an xor with a mask, e.g.:

> math --base=hex bitxor 0x0F, 0xFF
0xF0

> math --base=hex bitxor 0x2, 0x3
# Here we mask with 0x3 == 0b111, so our number is 3 bits wide
# Only the 1 bit isn't set.
0x1
  • ceil โ€” round number up to the nearest integer
  • cos โ€” the cosine
  • cosh โ€” hyperbolic cosine
  • exp โ€” the base-e exponential function
  • fac โ€” factorial โ€” also known as x! (x * (x - 1) * (x - 2) * ... * 1)
  • floor โ€” round number down to the nearest integer
  • ln โ€” the base-e logarithm
  • log or log10 โ€” the base-10 logarithm. to compute the logarithm for an arbitrary base b, use log(x) / log(b).
  • log2 โ€” the base-2 logarithm
  • max โ€” returns the largest of the given numbers โ€” this takes an arbitrary number of arguments (but at least one)
  • min โ€” returns the smallest of the given numbers โ€” this takes an arbitrary number of arguments (but at least one)
  • ncr โ€” "from n choose r" combination function โ€” how many subsets of size r can be taken from n (order doesn't matter)
  • npr โ€” the number of subsets of size r that can be taken from a set of n elements (including different order)
  • pow(x,y) โ€” returns x to the y (and can be written as x ^ y)
  • round โ€” rounds to the nearest integer, away from 0
  • sin โ€” the sine function
  • sinh โ€” the hyperbolic sine
  • sqrt โ€” the square root โ€” (can also be written as x ^ 0.5)
  • tan โ€” the tangent
  • tanh โ€” the hyperbolic tangent

all of the trigonometric functions use radians (the pi-based scale, not 360ยฐ). sorry to everyone who thinks in degrees, she said no. ๐Ÿ“๐Ÿ’…


๐Ÿ“ EXAMPLES ๐Ÿ“

  • math 1+1 outputs 2. iconic. โœจ
  • math $status - 128 outputs the numerical exit status of the last command minus 128.
  • math 10 / 6 outputs 1.666667.
  • math -s0 10.0 / 6.0 outputs 1.
  • math -s3 10 / 6 outputs 1.667.
  • math "sin(pi)" outputs 0.
  • math 5 \* 2 or math "5 * 2" or math 5 "*" 2 all output 10.
  • math 0xFF outputs 255, math 0 x 3 outputs 0 (because it computes 0 multiplied by 3).
  • math bitand 0xFE, 0x2e outputs 46.
  • math "bitor(9,2)" outputs 11.
  • math --base=hex 192 prints 0xc0.
  • math 'ncr(49,6)' prints 13983816 โ€” that's the number of possible picks in 6-from-49 lotto. ๐Ÿ‘‘
  • math max 5,2,3,1 prints 5.

๐Ÿงฎ COMPATIBILITY NOTES ๐Ÿงฎ

fish 1.x and 2.x releases relied on the bc command for handling math expressions. starting with fish 3.0.0, fish uses the tinyexpr library and evaluates the expression without the involvement of any external commands. glow-up of the century, honestly. ๐Ÿ”ฅ

you don't need to use -- before the expression, even if it begins with a minus sign which might otherwise be interpreted as an invalid option. if you do insert -- before the expression, it will cause option scanning to stop just like for every other command, and it won't be part of the expression. ๐Ÿซถ


fish-shell developers


and that's the page, bestie!! ๐ŸŽ€ authored by the fish-shell developers, version 4.9, dated Sep 08, 2026 โ€” go forth and do math, you absolute main character ๐Ÿฆ‹๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: fish 4.9.3-1 ยท GPL-2.0-only AND BSD-3-Clause AND ISC AND MIT AND PSF-2.0
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms