/errors/bash/integer-expression-expected
bash: [: integer expression expected
- 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
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.
Related message: unary operator expected
An empty, unquoted variable disappears from the command line, so [ $x -eq 1 ] becomes [ -eq 1 ]:
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
| Goal | Use |
|---|---|
| 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.