Multiplication on command line terminal

2019-01-21 01:49发布

I'm using a serial terminal to provide input into our lab experiment. I found that using

$ echo "5X5"

just returns a string of "5X5". Is there a command to execute a multiplication operation?

8条回答
手持菜刀,她持情操
2楼-- · 2019-01-21 02:43

A simple shell function (no sed needed) should do the trick of interpreting '5X5'

$ function calc { bc -l <<< ${@//[xX]/*}; };
$ calc 5X5
25
$ calc 5x5
25
$ calc '5*5'
25
查看更多
神经病院院长
3楼-- · 2019-01-21 02:45

Internal Methods

Bash supports arithmetic expansion with $(( expression )). For example:

$ echo $(( 5 * 5 ))
25

External Methods

A number of utilities provide arithmetic, including bc and expr.

$ echo '5 * 5' | /usr/bin/bc
25

$ /usr/bin/expr 5 \* 5
25
查看更多
登录 后发表回答