Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to perform mathematical operations through shell

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to carry out mathematical operations through shell". In daily operation, I believe many people have doubts about how to carry out mathematical operations through shell. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to carry out mathematical operations through shell"! Next, please follow the small series to learn together!

In Bash, bash math is a bit awkward, it is difficult to adapt and remember, had to write a blog post, convenient later flip through. There are four ways to do math:

I. Let's order

The code is as follows:

#/bin/bash

num1=13

num2=14

let sum=$num1+$num2

echo $sum

#Self-increase

let sum++

#Self-reduction

let sum--

#Abbreviations

let sum+=1

let sum-=2

#By the way, let sum=(1+3)*(2+2) actually doesn't work, it really sucks!

2.$[] form

The code is as follows:

#!/ bin/bash

sum = $[99+88]

#[] also uses variables

num1=11

num2=22

sum=$[$num1+$num2]

III.$(()) method

The code is as follows:

#!/ bin/bash

sum=$((1+2))

# $(()) can use () for priority calculation arrangement

sum=$(( (1+2)*3 ))

echo $sum #9

4. You can also use the expr command. This command requires that the operand and the operand are empty.

The code is as follows:

#!/ bin/bash

expr 3 + 4 #7

sum=`expr 33 + 44`#33 followed by spaces,+ followed by spaces, if sum=`expr 3+4` were written echo $sum would be 33+44

echo $sum #77

The above four methods do not support floating-point operations. To perform floating-point operations, you should use the bc command. The syntax format is relatively simple:

The code is as follows:

#!/ bin/bash

sum=`echo 222.222+333.333 | bc`

echo $sum

#sum=`echo 12.228222+(22222*2) |BC`actually can't, decisively despise it.

At this point, the study of "how to do mathematical operations through shell" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report