# FreeBSD Manual Pages

GEXPR(1) General Commands Manual GEXPR(1)NAMEgexpr - handy shell calculatorSYNOPSISgexpr[ options ]expressionDESCRIPTIONgexpris an expression parser that can be used as a simple command-line calculator, as ingexpr'sin(pi/4)*sqrt(4)', or to add floating point math to shell scripts. It is meant to be an alternative tobc(1), be- ing less powerful but lighter and much more intuitive. It also provides a few nice features of its own. If an expression is given as argument, it must be protected from the shell using quotes as in the example above. If you fail to do so, parentheses will be interpreted and the * character will be expanded by the shell, wreaking havoc. Using double quotes " " is necessary if you want to use shell variables withinexpression.gexprsupports the usual arithmetical operators+-*/, the rela- tional operators _ _=_ _===!=, and all the standard C mathematical functions apart fromfrexp(3) andmodf(3), which cannot be fully im- plemented since they actually return two values. In addition,gexprprovides the constants defined in math.h (M_PI, etc), thefact(n)func- tion, which returns the factorial ofn, and thernd(n)function, which returns a random number between 0 andn. A nice feature ofgexpris the possibility of using other bases than 10. For instance, this expression is allowed: $ gexpr "0x10 + 0b1010 + 010" 34 $ _ the prefix "0x" denotes numbers written in base 16, "0b" numbers in base 2, and "0" octal numbers. The command "base nn" is used to display the results in a base between 2 and 16. Example: gexpr> base 16 output base is now 16 gexpr> 256 * 2 200 gexpr> _ The command "decimals nn" (or "dec nn") specifies the number of decimal positions. Example: gexpr> PI 3.1415926525 gexpr> dec 20 decimal positions now 20 gexpr> PI 3.14159265258979311740 gexpr> _COMMANDSbasespecify the output base.dec(imals)specify the number of decimals (default: 10).helpdisplay a list of functions, constants, and commands.q(uit)quit the program.OPTIONS--help,-hdisplay a short help.--basen,-bnoutput results in base n.--no_prompt,-ndon't display the gexpr> prompt.EXAMPLESgexpr 2 + 10 / 2 gexpr "sqrt(5) < log(10)" echo "sqrt(2)/2" | gexpr -n gexpr "sin($X) - tan($Y)" gexpr "$X + ($Y)*log10(${ZZ})" This is an interesting use ofgexprin a shell script: #!/bin/sh DEC=`echo "M_PI_2" | gexpr -n` EXA=`echo "M_PI_2" | gexpr -n -b16` echo "Pi/2 is $DEC (or $EXA in hexa)" Another example: #!/bin/sh X=0 while [ `gexpr "$X < 10"` = 1 ] do X=`gexpr "$X + 0.2"` echo $X doneBUGSIt is awfully slow and its use cannot replace a "real" programming lan- guage supporting floating point math. Most errors are trapped but some are not, like overflows and under- flows. For example, on the Linux box it was written ongexproverflows when the result exceeds about 1.797e+308. It would be nice to add command-line editing. This would makegexprquite bigger, though.AUTHORGuido Gonzato <ggonza@tin.it>SEE ALSOexpr(1),sh(1),bc(1) GEXPR 2.0.2 May 2001 GEXPR(1)

NAME | SYNOPSIS | DESCRIPTION | COMMANDS | OPTIONS | EXAMPLES | BUGS | AUTHOR | SEE ALSO

Want to link to this manual page? Use this URL:

<https://www.freebsd.org/cgi/man.cgi?query=gexpr&sektion=1&manpath=FreeBSD+12.2-RELEASE+and+Ports>