Skip to content

/errors/bash/integer-expression-expected

bash: [: integer expression expected

verified onenvironmentlocal · linux 6.18.44-fc-v140 x64 · GNU bash 5.2.21updated
tool
Bash
exit code
2
versions
Bash 4+, Bash 5.x
os
linux, macos

[: abc: integer expression expected means a test such as [ "$x" -eq 1 ] received a value that is not a whole number. The operators -eq, -ne, -lt, -le, -gt and -ge compare integers only. Validate the value first, or compare it as a string with =. The test returns exit status 2 when it fails this way.

Reproduce it

bash
x=abc
[ "$x" -eq 1 ] 2>&1
echo "status: $?"

Output

bash: line 2: [: abc: integer expression expected
status: 2

The message starts with bash: line N: because the runner uses bash -c. In a script file, the file name replaces bash, and in an interactive shell it reads bash: [: abc: integer expression expected.

An empty, unquoted variable disappears from the command line, so [ $x -eq 1 ] becomes [ -eq 1 ]:

bash
x=
[ $x -eq 1 ] 2>&1
echo "status: $?"

Output

bash: line 2: [: -eq: unary operator expected
status: 2

Quote the variable ("$x") in every test. With quotes, the empty value reaches [ as an empty argument and you get integer expression expected instead, which is clearer.

Safe comparison patterns

GoalUse
Value is a whole number[[ $x =~ ^[0-9]+$ ]]
Integer comparison with a default(( ${x:-0} == 1 ))
Compare text[ "$x" = "abc" ]

Bash arithmetic cannot handle decimals such as 1.5. Use awk or bc for those.

Sources