Adding and dividing variables in bash. error: TEST3=: command not found

Learn adding and dividing variables in bash. error: test3=: command not found with practical examples, diagrams, and best practices. Covers bash, variables, disk development techniques with visual ...

Bash Arithmetic: Understanding 'command not found' Errors with Variables

Hero image for Adding and dividing variables in bash. error: TEST3=: command not found

Learn how to correctly perform arithmetic operations (addition, division) on variables in Bash and troubleshoot common 'command not found' errors.

Bash scripting is a powerful tool for automation and system administration. However, performing arithmetic operations on variables can sometimes lead to unexpected errors, especially for newcomers. A common issue encountered is the TEST3=: command not found error, or similar messages, when attempting to add or divide variables. This article will demystify Bash arithmetic, explain why these errors occur, and provide the correct methods for performing calculations.

The Root Cause: Bash's String-Oriented Nature

Unlike many other programming languages, Bash treats variables as strings by default. When you write TEST3=$TEST1 + $TEST2, Bash doesn't interpret + as an arithmetic operator. Instead, it tries to execute TEST3=value_of_TEST1 as a command, followed by + as another command, and then value_of_TEST2 as a third command. Since + is not a recognized command, you get the command not found error. The same logic applies to division (/) and other arithmetic operators.

flowchart TD
    A[User attempts: TEST3=$TEST1 + $TEST2]
    B{Bash interprets 'TEST3=$TEST1'}
    C{Bash interprets '+'}
    D{Bash interprets '$TEST2'}
    B --> E[Assigns string 'value_of_TEST1' to TEST3]
    C --> F["Attempts to execute '+' as a command"]
    F --> G["Error: '+: command not found'"]
    D --> H["Attempts to execute 'value_of_TEST2' as a command"]
    G --> I[Script Fails]
    H --> I

How Bash interprets incorrect arithmetic syntax, leading to 'command not found' errors.

Correct Methods for Bash Arithmetic

Bash provides specific constructs for performing arithmetic operations. The two primary methods are ((...)) for integer arithmetic and expr for more general expressions, though ((...)) is generally preferred for its simplicity and power.

Method 1: Using ((...)) for Integer Arithmetic

The ((...)) construct evaluates arithmetic expressions. Variables inside ((...)) do not need to be prefixed with $ for their values to be used, though it's harmless to include them. This method supports standard arithmetic operators like +, -, *, /, % (modulo), and ** (exponentiation).

#!/bin/bash

TEST1=10
TEST2=5

# Addition
(( TEST3 = TEST1 + TEST2 ))
echo "Addition (TEST1 + TEST2): $TEST3" # Output: 15

# Division
(( TEST4 = TEST1 / TEST2 ))
echo "Division (TEST1 / TEST2): $TEST4" # Output: 2

# Division with floating point (Bash only supports integer arithmetic in ((...)))
# This will truncate the result
TEST5=7
TEST6=2
(( TEST7 = TEST5 / TEST6 ))
echo "Division (7 / 2): $TEST7" # Output: 3 (truncated)

# Using $ prefix (also works)
(( TEST8 = $TEST1 * $TEST2 ))
echo "Multiplication (TEST1 * TEST2): $TEST8" # Output: 50

Correctly performing addition and division using ((...)) in Bash.

Method 2: Using expr (Older Method)

The expr command evaluates an expression and prints the result to standard output. It requires spaces between operators and operands, and operators like * and / often need to be escaped or quoted because they have special meaning to the shell.

#!/bin/bash

TEST1=10
TEST2=5

# Addition
TEST3=$(expr $TEST1 + $TEST2)
echo "Addition (TEST1 + TEST2): $TEST3" # Output: 15

# Division (note the escaped /)
TEST4=$(expr $TEST1 / $TEST2)
echo "Division (TEST1 / TEST2): $TEST4" # Output: 2

# Division with floating point (expr also only supports integer arithmetic)
TEST5=7
TEST6=2
TEST7=$(expr $TEST5 / $TEST6)
echo "Division (7 / 2): $TEST7" # Output: 3 (truncated)

Performing arithmetic using the expr command.

Handling Floating-Point Arithmetic

Both ((...)) and expr are limited to integer arithmetic. If you need floating-point calculations, you'll need to use external utilities like bc (basic calculator) or awk.

#!/bin/bash

TEST1=10
TEST2=3

# Using 'bc' for floating-point division
# 'scale=2' sets the number of decimal places
RESULT_BC=$(echo "scale=2; $TEST1 / $TEST2" | bc)
echo "Floating-point division (bc): $RESULT_BC" # Output: 3.33

# Using 'awk' for floating-point division
RESULT_AWK=$(awk "BEGIN { print $TEST1 / $TEST2 } ")
echo "Floating-point division (awk): $RESULT_AWK" # Output: 3.33333

Performing floating-point arithmetic using bc and awk.

1. Identify the Error

When you see command not found or similar errors during arithmetic, it's a strong indicator that Bash is trying to execute parts of your expression as commands.

2. Choose the Right Tool

For integer arithmetic, use ((...)). For floating-point arithmetic, use bc or awk.

3. Implement Correct Syntax

Rewrite your arithmetic expressions using the appropriate syntax for ((...)), expr, bc, or awk.

4. Test Your Script

Run your modified script to ensure the calculations are performed correctly and the errors are resolved.