How to Argument String to Integer in Bash?

In Bash, there is no date type for declaring variables. It takes everything as a character string, prints it in the output, and does not perform its arithmetic operations. Moreover, an error is generated when performing the arithmetic operations in bash. For this purpose, Linux offers built-in command line tools to convert the argument string to an integer easily.

This post lists the possible ways to convert an argument string to Integer in Bash:

Method 1: Using the “$((…))” Arithmetic Expansion Operator

The arithmetic expansion operator “$((…))” translates the string into the numerical expression. It is used instead of backticks and double quotes that perform the same job. 

Let’s see how it converts a string to an integer in bash.

Example: Converting Argument String to Integer With “$((…))” Operator

Create or open the bash script named “code.sh” in the “nano” text editor:

$ nano code.sh

Code

#!/bin/bash
a=2
b=7
c=$((a+b))
echo "$c"

The above code description is as follows:

  • The “#! /bin/bash” (Bash Shebang) instructs the shell to execute the script in “bash.”
  • The “a” and “b” two variables are declared, i.e., “a=2” and “b=7”.
  • The “c” variable is declared that contains the sum of “a” and “b” enclosed in the arithmetic expansion operator, i.e., $((a+b)).
  • Finally, the “echo” command will print the “$c” variable value on the terminal.

Press “Ctrl+S” to save and “Ctrl+X” to exit the editor.

Execute the Script

Run the created script and get its results in the terminal:

$ ./code.sh

The “code.sh” script has been successfully executed and returned the sum of the “a+b” variables as “9”.

Method 2: Using the “expr” Command

The “expr” is a command line utility that evaluates the specified expression and returns its output. It is used to perform arithmetic operations and string operations in bash, such as finding characters in a string, the length of the string for comparison, finding the substring, and much more. 

Let’s see how it can convert the argument string to an integer in bash.

Example: Converting Argument String to Integer With “expr” Command

The method 1 “code.sh” script is taken in this example and modified accordingly.

Code

#!/bin/bash
a=2
b=7
c=`expr $a + $b`
echo "$c"

At this time, the “c” variable value is enclosed in `expr $a + $b` format:

  • The backticks“ are used to execute the external “expr” command also called command substitution.
  • The “expr” will take the string “$a” and “$b” integer value and perform the addition operation on it:

Execute the “code.sh” script and check its output:

$ ./code.sh

The output is the same as method 1, which confirms that the “expr” has successfully converted the string to an integer value.

Method 3: Using the “let” Command

The “let” is the simple built-in command line tool that evaluates the arithmetic expressions in bash. It works the same as double parenthesis and the “expr” command. Let’s see its practical implementation.

Example: Converting Argument String to Integer With “let” Command

The “let” command is specified in the “code.sh” script for declaring the string:

Code

#!/bin/bash
a=2
b=7
let c=a+b
echo "$c"

In the above code block, the “let” command will evaluate additional arithmetic operations and return the output to the variable “c”:

The code .sh execution provides the following output:

$ ./code.sh

The output shows the successful conversion of a string to an integer.

Method 4: Using the “bc” Command

The “bc” command line tool acts as a calculator that performs basic mathematical operations like addition, subtraction, etc. Like the “expr” command, it can also be used in a bash script to evaluate arithmetic expressions.

This method works with the help of an example to convert an argument string to an integer.

Example: Converting Argument String to Integer With “bc” Command

For instance, the “code.sh” script is modified with the addition of the “bc” command line tool: 

Code:

#!/bin/bash
a=2
b=7
echo $a+$b | bc

The “bc” command is piped with the “echo” command:

  • The “echo” command will print the string value “2+7”.
  • The pipe character “|” will pipe the input to the “bc” command that will calculate the sum and will return the correct answer:

Run the “code.sh” script in the terminal:

$ ./code.sh

The “bc” tool has successfully calculated the string value.

Method 5: Using the “awk” Command

The “awk” command searches the text pattern from the file and performs specific operations like add, replace, and print. It can also evaluate the arithmetic expressions in the bash script.

Example: Converting Argument String to Integer With “awk” Command

In this scenario, the combination of “echo” and “awk” commands is used in the “code.sh” script: 

Code:

#!/bin/bash
a=2
b=7
echo $a $b | awk '{print $1 + $2}'

In the above code block:

  • The “echo” command will print the string “$a” and “$b” values as “2” and “7”.
  • The pipe character “|” will pipe the input to the “awk” command that takes the reference value of $1(for $a) and $2(for $b) and display the result after the addition:

Execute the script in this way:

$ ./code.sh

The “awk” command has done its job successfully.

Conclusion

In Linux, the argument string can be easily converted to an integer using the “$((…))” arithmetic expansion operator, “expr,” “let,” “bc,” and “awk” commands. These utilities evaluate the string as an expression and return the integer value as output.

This post has covered all possible aspects of argument string to integer in Bash.