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
Running Regular Expressions in Case Statements in Bash
Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM
-
This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters
Writing to a File in Bash
Publish Date:2025/03/22 Views:133 Category:OPERATING SYSTEM
-
This article will let us explore different ways to write files in bash. Files can be appended or overwritten as required. Let's see how we can do it. Different ways to write/overwrite files in Bash We will see several operators, such as and
Terminating a Process in Bash
Publish Date:2025/03/22 Views:135 Category:OPERATING SYSTEM
-
This article will first discuss the different concepts related to Linux processes. After this, we will learn the different ways to terminate a process. Before going into the kill command, we must understand some preliminary concepts. Simple
Solve R command not found on Bash (or Cygwin)
Publish Date:2025/03/21 Views:102 Category:OPERATING SYSTEM
-
Commands may sometimes behave differently than you expect, even if it appears that you have done everything right, such as in the case of bash: '\r': command not found or similar error messages, for example syntax error near unexpected toke
How to fix the error Make Command Not Found in Cygwin
Publish Date:2025/03/21 Views:120 Category:OPERATING SYSTEM
-
Cygwin allows Windows users to access certain Linux features and includes a large number of GNU and open source tools that are commonly found in popular Linux distributions. When using Cygwin, it is easy to encounter a command not found err
Error handling in Bash
Publish Date:2025/03/21 Views:94 Category:OPERATING SYSTEM
-
This article introduces error handling in bash. Remember, understanding exit codes, options such as errexit and trap allow us to build robust scripts and manage bash problems more effectively. Exit Codes in Bash Handling errors based on exi
Getting the absolute path in Bash
Publish Date:2025/03/21 Views:60 Category:OPERATING SYSTEM
-
In this Bash article, we will learn different ways to get the absolute path in Linux. We will also learn some different Linux commands to get the absolute path of a file. Before we begin, we need to understand the basic concepts of absolute
Difference between Bash Nohup and &
Publish Date:2025/03/21 Views:151 Category:OPERATING SYSTEM
-
This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux Processes in the Backgr
Renaming Files in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
With the help of Bash scripts, you can automate your tasks. File renaming is a common task on various systems. You can rename all the files manually. However, if your file names have a sequence, it is better to automate this task. This way