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
Splitting a string into variables in Bash
Publish Date:2025/03/23 Views:58 Category:OPERATING SYSTEM
-
This article will discuss different ways to split a string into variables in Bash. We will start our discussion with a brief introduction to strings. Later, we will discuss various ways to split a string using Bash examples. Strings in Bash
Bash remove newline from string
Publish Date:2025/03/23 Views:184 Category:OPERATING SYSTEM
-
This article explains how to remove newlines from a string in Bash. Creating a string with newlines in Bash Sometimes, it is required that a string does not have newline characters in it. Bash provides several methods to remove newline char
Remove the first character from a string in Bash
Publish Date:2025/03/23 Views:61 Category:OPERATING SYSTEM
-
Removing the first character of a string can be tricky in Bash because there is no built-in function that can do this directly. However, there are several ways to achieve this, such as using the sed command, the cut command, or substring pa
Replace characters in a string using Bash
Publish Date:2025/03/23 Views:84 Category:OPERATING SYSTEM
-
A common operation involving string literals is replacing individual characters or substrings within the string with other characters or substrings. In this article, we will look at several ways to do string replacement using the BASH shell
String comparison operators in Bash
Publish Date:2025/03/23 Views:99 Category:OPERATING SYSTEM
-
In this article, we will explain string comparison in Bash using if statement. The shell program that runs in Linux and provides a command line interface for users to execute different commands is called Bash shell. It is also used as the d
Get the length of a string in Bash
Publish Date:2025/03/23 Views:145 Category:OPERATING SYSTEM
-
In programming languages, the length or size of a data type plays an important role. It helps in list traversal and facilitates the extraction of useful information. The length is especially important when performing tasks that require trav
使用 Bash 替换字符串中的字符
Publish Date:2023/05/18 Views:163 Category:操作系统
-
在本文中,我们将研究使用 BASH shell 进行字符串替换的几种方法。涉及字符串文字的一种常见操作是用其他字符或子字符串替换该字符串中的单个字符或子字符串。
Bash 中的字符串比较运算符
Publish Date:2023/05/18 Views:219 Category:操作系统
-
在本文中,我们将使用 if 语句解释 Bash 中的字符串比较。运行在 Linux 中,提供命令行界面供用户执行不同命令的 shell 程序称为 Bash shell。
在 Bash 中将字符串转换为整数
Publish Date:2023/05/18 Views:168 Category:操作系统
-
本篇文章将讨论 Bash 脚本中的字符串到整数的转换。 首先,我们将讨论字符串的数学运算问题,然后我们将了解如何将字符串转换为整数。