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 bc tools in linux

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Editor to share with you how to use bc tools in linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Bc tools provide you with features that meet your expectations of a scientific calculator, a financial calculator, or a simple calculator. In addition, it can be scripted from the command line if necessary. This allows you to use it in shell scripts when you need to do complex math.

Because bc is also used in other system software, such as CUPS printing service, it may already be installed on your Fedora system. You can use the following command to check:

Dnf list installed bc

If for some reason you don't see it in the output of the above command, you can use the following command to install it:

Sudo dnf install bc

Do some simple mathematical operations with bc

One way to use bc is to enter its own shell. There you can do many calculations according to the line. When you type bc, the first warning about the program appears:

$bc bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'.

Now you can follow one input expression or command per line:

1-1

Bc will answer the above formula. The answer is:

two

There are other orders you can carry out here. You can use addition (+), subtraction (-), multiplication (*), division (/), parentheses, exponential symbols (^), and so on. Note that bc also follows all established rules of operation, such as the order of operations. You can try the following example:

(4 / 7) * 2 / 4 / 7 / 2

To exit bc, you can send an "input end" signal to bc by pressing the key combination Ctrl+D.

Another way to use bc is to use the echo command to pass an expression or command. The following example is the "Hello, world" example in the calculator, which uses the pipe function (|) of shell to pass the output of echo into bc:

Echo '1room1' | bc

Using shell's pipeline, you can send more than one operation, and you need to use a semicolon to separate different operations. The results will be returned in different rows.

Echo '1mm 1; 2m 2' | bc

Precision.

In some calculations, bc uses the concept of precision, the number of digits after the decimal point. The default precision is 0. Division always uses precision settings. So, if you don't set the precision, it may lead to unexpected answers:

Echo '3swap 2' | bc echo' scale=3; 3gamer 2' | bc

Multiplication uses a more complex precision selection mechanism:

Echo '3room2' | bc echo' 3room2.0' | bc

At the same time, the related operations of addition and subtraction are similar:

Echo'7-4.15' | bc

Other binary systems

Another useful feature of bc is the ability to use counting systems other than decimal. For example, you can easily do hexadecimal or binary mathematical operations. You can use the ibase and obase commands to set the input and output binary systems, respectively. It is important to keep in mind that once you use ibase, any numbers you enter will be considered to be in the newly defined binary system.

To convert or calculate hexadecimal to decimal numbers, you can use a command similar to the following. Note that hexadecimal numbers greater than 9 must be uppercase (Amurf):

Echo 'ibase=16; A42F' | bc echo' ibase=16; 5F72C39B' | bc

To make the result hexadecimal, you need to set obase:

Echo 'obase=16; ibase=16; 5F72C39B' | bc

Here is a little trick. If you do these hexadecimal operations in shell, how can you make the input decimal again? The answer is to use the ibase command, but you must set it to a value equivalent to 10 in the decimal system. For example, if ibase is set to hexadecimal, you need to enter:

Ibase=A

Once you have executed the above command, all input numbers will be decimal, and then you can type obase=10 to reset the output system.

These are all the contents of this article entitled "how to use bc tools in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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

Servers

Wechat

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

12
Report