Floating-point arithmetic in Bash
This short article describes how to quickly perform floating-point calculations in GNU BASH (shell scripts) directly at the command prompt or in a shell script.
If you work with numbers, it can be helpful to perform quick floating-point calculations in the command prompt (shell) or in a shell script. This article describes the following four ways to do floating-point arithmetic in Bash:
- Arbitrary precision calculator method using the bc command.
- Use awk mode scanning and processing method.
- Use the perl command method.
- Use the python command method.
Integer Arithmetic in Bash
Integer-only arithmetic can be easily performed in a Bash script using the expr command on the command line or square brackets to evaluate the expression $[1+1]. This is shown in the following code:
#!/bin/bash
echo $[2 + 1]
This will generate the following output:
But these are only integer calculations. If the answer is a floating point number in these calculations, then it only displays the integer part of it.
To perform floating point arithmetic, we need the help of the tools discussed below.
Floating-point arithmetic in Bash
There are a variety of tools available for performing floating-point arithmetic in Bash. However, this article will explore four of the most widely used and readily available (in any UNIX or Linux operating system family).
Floating-point arithmetic in Bash using the basic calculator (bc)
For a command line calculator, use the bc command. It is equivalent to a simple calculator that we can use to perform simple mathematical calculations.
The most basic operation in any programming language is arithmetic. The bc and expr commands available in Linux or Unix operating systems are used to perform mathematical operations.
These commands evaluate arithmetic expressions in shell scripts or Bash.
The following script will calculate the addition and division of floating point numbers and display the results on the screen.
#!/bin/bash
echo "Addition: "
echo '1.5 + 2.5' | bc -l
echo "Division"
echo '2.1/3.2' | bc -l
This will give the following output:
Using awk command in Bash for floating point operations
A real or floating point number contains a decimal part. In awk, all numeric values are represented by double-precision floating point numbers.
In other words, all numbers in awk are floating point numbers, which means that all calculations use these numbers.
The good thing about awk command is that it is available in all UNIX-like operating systems or Linux distributions since it is quite old and has been used for a long time.
The following script uses awk to calculate the multiplication and division of two floating point numbers.
#!/bin/bash
echo "Multiplication: "
echo - | awk '{print 2.1 * 3.2}'
echo "Division"
echo - | awk '{print 2.1 / 3.2}'
This will give the following output:
Using perl commands in Bash for floating point operations
Perl is a programming language that is usually included in the software packages of all Linux distributions or other UNIX-like operating systems. The perl command can be used in Bash and it helps in performing floating point arithmetic operations in Linux.
It can perform all operations like addition, subtraction, multiplication, division, and assignment operators.
The following script uses the perl command in Linux to calculate the subtraction and division of two floating point numbers:
#!/bin/bash
echo "Subtraction: "
perl -e 'print 4.1 - 6.2'
echo "Division"
perl -e 'print 4.1 / 2.2'
This will give the following output:
Using Python commands in Bash for floating point operations
Just like Perl, Python is another language that is widely used in all areas of programming. It is a frequently used language that sometimes comes pre-installed in your Linux distribution.
The python command helps to perform floating point arithmetic in Bash scripts. It can perform all operations like addition, subtraction, multiplication, division, and assignment operators.
The following script will use Python commands to calculate the addition and division of two floating point numbers:
#!/bin/bash
echo "Addition: "
python -c 'print 4.1 + 6.2'
echo "Division"
python -c 'print 4.1 / 2.2'
This will give the following output:
Summarize
There are at least four ways to perform floating-point arithmetic on the command line or in a script under Unix or the GNU Bourne Again Shell (Bash). These are awk, perl, python, and the bc command.
Unix systems may come with AWK, BC, and Perl preinstalled. Although Python is still not as popular as PERL, it is now quite common.
Unlike other calculators, which typically default to 32-bit or 64-bit precision floating point, BC has the unique advantage of being an arbitrary precision calculator.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u