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,xcan 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 ๐
-sN or--scaleN โ 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. so3/2returns1by default, rather than2which1.5would normally round to. this is for compatibility withbc, 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. โญ-bBASE or--baseBASE โ sets the numeric base used for output (mathalways 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 a0xprefix. octal numbers will have a prefix of0but aren't understood bymathas input. ๐-mMODE or--scale-modeMODE โ sets scale behavior. the MODE can betruncate,round,floor,ceiling. the default value of scale mode isroundwith non zero scale andtruncatewith zero scale. ๐ฆ-hor--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*orxโ for multiplication.*is the glob character and needs to be quoted or escaped;xneeds to be followed by whitespace or it looks like0xhexadecimal 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 numberpiโ ฯ, 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 signacosโ arc cosineasinโ arc sineatanโ arc tangentatan2โ arc tangent of two variablesbitand,bitorandbitxorโ perform bitwise operations. these will throw away any non-integer parts and interpret the rest as an int.
Note: ๐bitnotandbitnanddon't exist. this is because numbers inmathdon'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 integercosโ the cosinecoshโ hyperbolic cosineexpโ the base-e exponential functionfacโ factorial โ also known asx!(x * (x - 1) * (x - 2) * ... * 1)floorโ round number down to the nearest integerlnโ the base-e logarithmlogorlog10โ the base-10 logarithm. to compute the logarithm for an arbitrary baseb, uselog(x) / log(b).log2โ the base-2 logarithmmaxโ 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 asx ^ y)roundโ rounds to the nearest integer, away from 0sinโ the sine functionsinhโ the hyperbolic sinesqrtโ the square root โ (can also be written asx ^ 0.5)tanโ the tangenttanhโ 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+1outputs2. iconic. โจmath $status - 128outputs the numerical exit status of the last command minus128.math 10 / 6outputs1.666667.math -s0 10.0 / 6.0outputs1.math -s3 10 / 6outputs1.667.math "sin(pi)"outputs0.math 5 \* 2ormath "5 * 2"ormath 5 "*" 2all output10.math 0xFFoutputs255,math 0 x 3outputs0(because it computes0multiplied by3).math bitand 0xFE, 0x2eoutputs46.math "bitor(9,2)"outputs11.math --base=hex 192prints0xc0.math 'ncr(49,6)'prints13983816โ that's the number of possible picks in 6-from-49 lotto. ๐math max 5,2,3,1prints5.
๐งฎ 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. ๐ซถ
๐ Copyright ๐
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