How do I divide in the Linux console?

2019-02-01 19:51发布

I have to variables and I want to find the value of one divided by the other. What commands should I use to do this?

12条回答
手持菜刀,她持情操
2楼-- · 2019-02-01 20:39

I still prefer using dc, which is an RPN calculator, so quick session to divide 67 by 18 with 4 digits precision would look like

>dc
4k
67
18/p
3.7222
q
>

Obviously, much more available: man dc

查看更多
做自己的国王
3楼-- · 2019-02-01 20:44

you can also use perl -e

perl -e 'print 67/8'
查看更多
不美不萌又怎样
4楼-- · 2019-02-01 20:45

In the bash shell, surround arithmetic expressions with $(( ... ))

$ echo $(( 7 / 3 ))
2

Although I think you are limited to integers.

查看更多
Anthone
5楼-- · 2019-02-01 20:47

Something else you could do using raytrace's answer. You could use the stdout of another shell call using backticks to then do some calculations. For instance I wanted to know the file size of the top 100 lines from a couple of files. The original size from wc -c is in bytes, I want to know kilobytes. Here's what I did:

echo `cat * | head -n 100 | wc -c` / 1024 | bc -l
查看更多
爷、活的狠高调
6楼-- · 2019-02-01 20:48

You should try to use:

echo "scale=4;$variablename/3"|bc
查看更多
beautiful°
7楼-- · 2019-02-01 20:55

In bash, if you don't need decimals in your division, you can do:

>echo $((5+6))
11
>echo $((10/2))
5
>echo $((10/3))
3
查看更多
登录 后发表回答