Understanding and Fixing 'Integer Expression Expected' Error in Shell Scripts

Nov 23, 2025 · Programming · 8 views · 7.8

Keywords: Shell Script | Integer Expression Error | Bash Programming

Abstract: This article provides an in-depth analysis of the common 'integer expression expected' error in shell scripts, using a user age validation script as an example. It explains the root causes and presents multiple solutions, with a focus on best practices using double brackets [[ ]] for numerical comparisons. Additional insights include correct single bracket [ ] syntax and handling hidden characters. Through code examples and step-by-step explanations, readers will grasp shell script numerical comparison mechanisms, avoid common pitfalls, and enhance script robustness.

Problem Background and Error Analysis

In shell script programming, numerical comparisons are frequent operations, but beginners often encounter the "integer expression expected" error. Consider this typical user age validation script:

#!/bin/bash
echo " Write in your age: "
read age
if [ "$age" -le "7"] -o [ "$age" -ge " 65" ]
then
    echo " You can walk in for free "
elif [ "$age" -gt "7"] -a [ "$age" -lt "65"]
then
    echo " You have to pay for ticket "
fi

When executed, this script produces errors such as:

./bilet.sh: line 6: [: 7]: integer expression expected
./bilet.sh: line 9: [: missing `]'

These errors stem from two main issues: incorrect bracket usage without necessary spaces, and improper application of logical operators -o and -a within single brackets. The following sections dissect these problems and offer fixes.

Core Solution: Using Double Bracket Syntax

In Bash, double brackets [[ ]] offer enhanced conditional expression support, including direct use of logical operators && and ||. Here is the corrected script:

#!/bin/bash

echo " Write in your age: "
read age

if [[ "$age" -le 7 || "$age" -ge 65 ]] ; then
    echo " You can walk in for free "
elif [[ "$age" -gt 7 && "$age" -lt 65 ]] ; then
    echo " You have to pay for ticket "
fi

Key improvements in this approach include:

Double bracket syntax is recommended in Bash for its support of complex expressions and reduced error propensity.

Alternative Approach: Correct Single Bracket Usage

For POSIX-compliant shells, single brackets can be used with careful attention to syntax. The original script's flaws include:

A revised single bracket version is:

if [ "$age" -le "7" ] || [ "$age" -ge "65" ] ; then
    echo " You can walk in for free "
elif [ "$age" -gt "7" ] && [ "$age" -lt "65" ] ; then
    echo " You have to pay for ticket "
fi

This method works across all POSIX shells but is less concise than double brackets.

Handling Hidden Character Issues

Another common cause of the "integer expression expected" error is non-digit characters in variables, such as newlines \n or tabs. For instance, if the age variable contains 1234\n, comparisons will fail. The following code demonstrates cleaning the variable:

#!/bin/bash

# Simulate input with a newline
age='1234
'

# Remove hidden characters
age="${age//[$'\t\r\n ']}"

if [[ "$age" -gt 1000 ]] ; then
    echo "Valid number after cleaning"
else
    echo "Invalid input"
fi

Using the ${variable//pattern/} syntax removes specified characters, ensuring the variable is purely numerical. Enabling debug mode with set -xv can help identify hidden characters.

Summary and Best Practices

Numerical comparison errors in shell scripts often arise from improper syntax or unclean input. Recommended practices include:

By understanding these core concepts, developers can write more robust shell scripts and minimize runtime errors.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.