Convert a string to an integer in Bash
This article will discuss string to integer conversion in Bash script. First, we will discuss the math operations on strings and then we will see how to convert a string to an integer.
Mathematical operations on strings
Let's start our discussion with this simple code:
x_value=1000
y_value=20
echo $x_value+$y_value
The output of this code may be unexpected. See the output:
1000+20
The command echo concatenates the variable x_value and the variable y_value with a plus sign. Probably you are not interested in getting this output; instead, you expect 1020 as the output.
Having discussed this issue, let us explore ways to avoid it and get the desired output.
Use the expr command to convert a string to an integer
This command is used to evaluate an expression and get the result. For example:
$ expr 12 - 8
The output of this command will be 4. We can use this command to get the desired result:
x_value=1000
y_value=20
expr $x_value + $y_value
The output of this script is 1020; however, note that a space is required between the variable and the operator. Otherwise, this command will also perform concatenation with the same result.
Use the double bracket ((...)) construct to convert a string to an integer
The double-brace constructor (( ... ))
allows arithmetic expansion and evaluation; for example, x_value=$(( 6 - 2 ))
x_value will be set to 4. We can use this construction to get the desired result.
Let's look at an example script:
x_value=1000
y_value=20
echo $(( x_value * y_value ))
The output of this script will be 20000.
Use the Basic Calculator (bc) command to convert a string to an integer
In Bash, the basic calculator (bc) is used to perform basic arithmetic calculations directly from the Bash command interface.
For example, we could write:
$ echo "12-4" | bc
The output is 8. We can use this basic calculator to get the results we need.
The code to solve our original problem using bc is as follows:
x_value=1000
y_value=20
echo $x_value-$y_value | bc
The output is 980. In this expression, no spaces are required, as they are enforced in the expr command.
Convert a string to an integer using awk command
awk
is a precompiled command that allows the user to print formatted expressions containing variables, numeric functions, string functions, and logical operators. You can use this command in a number of ways.
For example, if you want to print a message with some formatted numeric calculations, you can use the echo command with the awk command, for example:
$ echo salary= 40000 10 | awk '{print $1 $2+$2*$3/100}'
In this command, we want to print the salary, where the salary includes a 10% bonus. Here, salary=
is our first parameter. We use it by writing $1.
Likewise, 40000 is the second argument used to write $2; similarly, $3 is a placeholder for the third argument (i.e., 10).
The output of this command is:
salary=44000
We can use the awk command to solve our original problem:
x_value=1000
y_value=20
echo $x_value $y_value | awk '{print $1 + $2}'
The output will be 1020, where x and y are the two parameters received by the awk command. Perform the calculation accordingly.
Use perl...print or perl...say to convert a string to an integer
We have print or say option in perl command. The difference between print and say is that print does not wrap, the next output will appear in the same line, whereas say command wraps.
For example:
x_value = 5
perl -E "print $x_value"
echo "*"
perl -E "say $x_value"
echo "*"
The output will be:
5*5
*
请注意
, the second asterisk is printed on the next line because the newline character is said rather than printed.
Therefore, we can use perl with print or say to get the output we need:
x_value=1000
y_value=20
perl -E "print $x_value+$y_value"
The output will be 1020.
Convert a string to an integer using Python
Python has a print statement that evaluates and prints an expression.
For example:
python -c "print 5 * 4"
The output will be 20. We can use this to get the desired result.
The code looks like this:
x_value=1000
y_value=20
python -c "print $x_value * $y_value"
The output is 20000.
We have various ways to convert a string into an integer variable to perform mathematical operations. Here, we have provided six different ways and you can use any of them according to your choice.
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
Hosting Docker Internal in Linux
Publish Date:2025/03/23 Views:143 Category:OPERATING SYSTEM
-
Docker allows developers to efficiently build, test, and deploy applications by packaging them in standardized units called containers. When working with Docker containers, you may encounter scenarios where you need to connect a container t
Setting the working directory in Docker
Publish Date:2025/03/23 Views:198 Category:OPERATING SYSTEM
-
If present, the working directory of a process in Compute is a directory in a linked hierarchical file system that is dynamic for each process. In Docker, we can set our working directory by editing the Dockerfile and adding the key WORKDIR
How to get IP address in CentOS
Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM
-
This short article is a brief introduction to CentOS followed by a brief discussion on how we can get the server IP address in CentOS using the Command Line Interface (CLI). This article will discuss some of the commands and their usage for
Updating YUM in Linux
Publish Date:2025/03/23 Views:148 Category:OPERATING SYSTEM
-
This article will teach us how to update YUM in Linux and how to install, update, remove, find and manage packages on a Linux system. We have also seen the difference yum update between and in Linux yum upgrade . yum update command in Linux
Installing Deb Files in Linux
Publish Date:2025/03/23 Views:93 Category:OPERATING SYSTEM
-
In this Linux article, we will learn how to install .deb (Debian Package) files on Linux systems. We will also see how to remove .deb files after installation. More importantly, we will learn different ways to install .deb files on Linux sy
lsof Command in Linux
Publish Date:2025/03/23 Views:100 Category:OPERATING SYSTEM
-
In this Linux article, we will learn about lsof command in Linux operating system. We will see how to use this command for different purposes in Linux. We use lsof the lsof command to verify the ports in use on the Linux operating system. U
How to solve the problem of not being able to execute binary files in Linux
Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM
-
In this article, we will learn how to execute binary files in Linux. We will also learn how to troubleshoot the error if Linux fails to execute the binary file. Usually, this error occurs when we run shell scripts in Linux. This article wil
Error in Linux Mesg: Ttyname Failed: Inappropriate Ioctl for Device Error
Publish Date:2025/03/23 Views:178 Category:OPERATING SYSTEM
-
In this article, we will learn how to fix the error mesg: ttyname failed: Inappropriate ioctl for device in Linux . We will discuss some of the causes of this error and show you how to fix it. Let's start with what causes this error in Linu
ps aux command in Linux
Publish Date:2025/03/23 Views:57 Category:OPERATING SYSTEM
-
If you are using Linux and are looking for a tool that can monitor all the processes running on your system, then you should use the command ps aux. This command will show you an overview of all running processes. It is very useful for trou