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 use the bc command of Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "how to use the bc command of Linux". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the bc command of Linux" can help you solve your doubts.

Bc command is a calculator language that supports interactive execution of arbitrary precision. Bash has built-in support for four operations of integers, but does not support floating-point operations, and the bc command can easily perform floating-point operations, and of course integer operations are no longer needed.

Enter bc from the terminal

Enter the bc command at the terminal, and then enter enter to enter the bc. Please see the figure below:

There are also some options for the bc command, which you may need, please see the table below.

Option description-h |-help help information-v |-version displays command version information-l |-mathlib uses standard math library-I |-interactive forced interaction-w |-warn displays POSIX warning message-s |-standard uses the POSIX standard to process-Q |-quiet does not display welcome information.

For example, if you don't want to display a bunch of useless information after entering the bc command, type bc-Q:

Using bc in an interactive environment

It is very easy to use bc for mathematical calculation, enter the mathematical expression as usual, and then press enter to see the result, please see the following figure.

It is worth mentioning that we defined a variable n, and then used n in the calculation, which shows that bc supports variables.

In addition to variables, bc supports common programming elements such as functions, loop structures, branch structures, and so on, which are similar to the syntax of other programming languages. Let's define a function for factorial:

In fact, we rarely use such a complex function, and in most cases we still use bc as an ordinary mathematical calculator to find out the value of the expression, so we don't have to dig deeper and understand it.

Built-in variable

Bc has four built-in variables, which we often use when calculating, as shown in the following table:

Variable names use scale to specify the precision, that is, the number of digits after the decimal point; the default is 0, that is, no decimal part is used. Ibase specifies the base of the number entered, which defaults to decimal. Obase specifies the base of the output number, which defaults to decimal. Last or. Represents the most recently printed number

[example 1] example of scale variable usage:

At the beginning, 10 scale 3 has a value of 3, with no decimal part, just because the default value of the scale variable is 0; later, you can see the decimal part by assigning a value greater than 0.

[example 2] examples of the usage of ibase and obase variables:

Note: obase should be placed in front of ibase as far as possible, because after ibase is set, the following numbers are converted to the base of ibase.

Built-in function

In addition to built-in variables, bc has some built-in functions, as shown in the following table:

The function name acts as s (x) to calculate the sine of x, where x is the arc value. C (x) calculates the cosine of x, where x is the arc value. A (x) calculates the arc tangent of x and returns the arc value. L (x) calculates the natural logarithm of x. E (x) to the x power of e. J (n, x) Bessel function, which calculates the order from n to x.

To use these mathematical functions, you need to use the-l option when entering the bc command, which means that the math library is enabled. Take a look at the following example:

Use multiple expressions in one line

In the previous example, we are basically one expression per line, which looks more comfortable; if you like, you can put multiple expressions on one line, as long as you use a semicolon; just separate them. Take a look at the following example:

Using the bc Calculator in Shell

In Shell scripts, we can use the bc calculator with the help of pipes or input redirection.

Pipe is a communication mechanism between Linux processes, which can use the output of the previous command (process) as the input of the next command (process), and the two commands are separated by vertical bar |. Typically, a command gets user input from the terminal and needs to be redirected if it is allowed to get input from somewhere else, such as a file.

Here we do not intend to start to explain the pipeline and redirection, do not understand the partners, please do their own Baidu.

Using bc Calculator with Pipelin

If the reader wants to output the results of the bc calculation directly, you can use the following form:

Echo "expression" | bc

Expression is the mathematical expression you want to evaluate, and it must conform to the syntax of bc, which we have already introduced. In expression, you can also use variables from Shell scripts.

The calculation result of bc can be assigned to the Shell variable using the following form:

Variable=$ (echo "expression" | bc)

Variable is the name of the variable.

[example 1] the simplest form:

[c.biancheng.net] $echo "3x8" | bc24 [c.biancheng.net] $ret=$ (echo "4y9" | bc) [c.biancheng.net] $echo $ret13

[example 2] use the variables in bc:

[c.biancheng.net] $echo "scale=4;3*8/7" | bc3.4285 [c.biancheng.net] $echo "scale=4;3*8/7;last*5" | bc3.428517.1425

[example 3] use the variables in the Shell script:

[c.biancheng.net] $Xero4 [c.biancheng.net] $echo "scale=5;n=$x+2;e (n)" | bc-l403.42879

In the second command, $x means to use the variable defined in the first Shell command, and n is the new variable defined in bc, which has nothing to do with the Shell script.

[example 4] binary conversion:

# Decimal to hexadecimal [mozhiyan@localhost] $mozhiyan@localhost 31 [mozhiyan@localhost ~] $nylon $(echo "obase=16;$m" | bc) [mozhiyan@localhost ~] $echo $n1F# hexadecimal to decimal [mozhiyan@localhost ~] $Macro1E [mozhiyan@localhost ~] $nforth $(echo "obase=10;ibase=16;$m" | bc) [mozhiyan@localhost ~] $echo $N30 use the bc Calculator with input redirection

You can assign the calculation result of bc to the Shell variable in the following form:

Variable=$ (bc

Where variable is the name of the Shell variable, express is the mathematical expression to be evaluated (you can wrap it in the same way it is written after entering bc), and EOF is the beginning and end of the mathematical expression (you can also change it to other names, such as aaa, bbb, etc.).

Take a look at the following example:

[c.biancheng.net] $echo 1E [c.biancheng.net] $nasty $(bc obase=10; > ibase=16; > print $m > EOF >) [c.biancheng.net] $echo $N30

If you have a lot of mathematical calculations, it is more convenient to use input redirection because mathematical expressions can be wrapped and written more clearly.

After reading this, the article "how to use the bc command of Linux" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.

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