Bash 5.3 changed the wording
On bash 5.3 the same error reads [: 5.0: integer expected. Bash 5.2 and older print integer expression expected. Same bug, same exit status 2.
A threshold check that worked for months starts taking the wrong branch. The value it compares came from a config file someone edited on Windows, a curl response, or a log line that now says 5.0 instead of 5. [ cannot compare it, prints integer expression expected, and returns 2. Inside an if that is false, so the script carries on as if the number were small.
What Does a Hidden Carriage Return Do to [ ]?
Reproduced on 2026-11-03 with a two-line config file saved with Windows line endings, read the way most scripts read a value:
Bash 5.2.21 (docker run --rm ubuntu:24.04), output piped through cat -A so the carriage return shows as ^M:
Command substitution strips the trailing newline, not the \r before it, so n is 5 plus a carriage return. In a terminal you do not see ^M. The \r moves the cursor back to column 0 and : integer expression expected is printed over the start of the line, so the file name and the value disappear from the message.
The surprise: bash 5.3 accepts it. The same script on Kali's bash 5.3.15 and on bash 5.3.9 in ubuntu:26.04:
dash 0.5.12, coreutils [ 9.10 and busybox 1.37 accept 5\r too. So a CRLF config file passes on a bash 5.3 workstation and fails on an Ubuntu 24.04 server. And [[ ]] and (( )) reject it on every version (the table below), so the same value works in one line of the script and breaks the next.
Which Values Trigger It, and What Does Each Shell Print?
Each value run through [ "$n" -gt 3 ], [[ $n -gt 3 ]] and (( n > 3 )), on bash 5.3.15 (Kali 2026.3), 5.3.9 (ubuntu:26.04) and 5.2.21 (ubuntu:24.04):
n | [ ] 5.2 | [ ] 5.3 | [[ ]] / (( )) |
|---|---|---|---|
$'5\r' | [: 5^M: integer expression expected, exit 2 | true, exit 0 | invalid arithmetic operator (error token is "^M"), exit 1 |
'' | [: : integer expression expected, exit 2 | [: : integer expected, exit 2 | no message, exit 1 |
5.0 | [: 5.0: integer expression expected, exit 2 | [: 5.0: integer expected, exit 2 | invalid arithmetic operator (error token is ".0"), exit 1 |
abc | [: abc: integer expression expected, exit 2 | [: abc: integer expected, exit 2 | no message, exit 1 |
" 5", "5 " | true, exit 0 | true, exit 0 | true, exit 0 |
The raw bash 5.3.15 run behind the 5.3 column and the [[ ]] column:
Two things in that run matter more than the message. [ ] returns 2 on bad input, but [[ ]] and (( )) return 1, the same status as an honest "not greater", so swapping brackets hides the error rather than fixing it. And [[ abc -gt 3 ]] prints nothing at all: inside arithmetic, a bare word is read as a variable name, and an unset variable is 0. Leading and trailing spaces are harmless in all three.
Other shells word it differently for the same value (the 5.0 case):
How Do I Fix integer expression expected?
Fix the value where it enters the script, then compare. Each of these was run on bash 5.3.15 and 5.2.21 with the same output.
1. Strip the carriage return when you read the value.
If the file itself is the problem, convert it once with dos2unix settings.conf or sed -i 's/\r$//' settings.conf. The CRLF bad interpreter page covers the same \r when it lands in a shebang instead.
2. Validate before comparing.
Allow a sign with ^-?[0-9]+$ if negatives are valid. Two traps showed up in the same run. A leading zero passes the regex and [ ], but [[ 08 -gt 3 ]] fails with value too great for base (error token is "08"), because arithmetic reads 08 as octal. Force base 10 with (( 10#$n > 3 )). And the shortcut ${n//[!0-9]/} (delete every non-digit) is not validation:
It fixes the carriage return and silently turns 5.0 into 50 and -3 into 3. Use it only on input you know is a whole number with junk around it.
3. Give an empty value a meaning. [ "${n:-0}" -gt 3 ] treats empty as zero. : "${n:?n must be set}" stops the script with a named error instead. The unary operator expected page has the full table for empty and unset variables.
4. Compare decimals with awk, not bash. Bash arithmetic is integer-only, in [ ], [[ ]] and (( )).
bc works on Kali ((( $(echo "5.0 > 3" | bc) )) returned 0), but in the ubuntu:24.04 image the same line printed bc: command not found. awk is always there: GNU Awk 5.3.2 on Kali, mawk 1.3.4 in ubuntu:24.04, same result. If you only need the whole part, ${n%.*} drops everything after the dot: 5.9 becomes 5.
Does ShellCheck Catch It?
Only when the decimal is in the source. ShellCheck 0.11.0 on three tests:
Line 5, if [ "$n" -gt 3 ], is not flagged, and cannot be: whether n holds 5\r is decided by the file at run time. A literal 3.5 gets SC2072, and > inside [ ] gets SC2071, which matters because [ "$n" > 3 ] is a redirect: run here with n=5, it returned true (exit 0) and left an empty file named 3 in the directory. For values from files, commands or users, the runtime check in fix 2 is the guard. Paste any other finding into the ShellCheck Error Decoder.
Prerequisites
bash (the message differs between 5.2 and 5.3, the fixes do not), awk for decimals. Tested on Kali 2026.3 (bash 5.3.15, dash 0.5.12, GNU coreutils 9.10, GNU Awk 5.3.2, bc 1.07.1, ShellCheck 0.11.0), ubuntu:24.04 (bash 5.2.21), ubuntu:26.04 (bash 5.3.9) and busybox:1.37 containers. No root.
Frequently Asked Questions
What does integer expression expected mean in bash?
It means the [ (test) command was asked to compare numbers with -eq, -ne, -lt, -le, -gt or -ge, and one side was not a whole number. The value is printed between the colons: [: 5.0: integer expression expected names 5.0, and [: : integer expression expected names an empty string. test exits with status 2, which an if or && treats as false.
Why does bash say integer expected instead of integer expression expected?
Bash 5.3 changed the wording. Bash 5.2.21 (Ubuntu 24.04) prints [: abc: integer expression expected; bash 5.3.9 (Ubuntu 26.04) and 5.3.15 (Kali) print [: abc: integer expected for the same line, with the same exit status 2. dash prints Illegal number, coreutils /usr/bin/test prints invalid integer, and busybox prints bad number.
Why does my number from a file fail the comparison when it looks fine?
The file almost certainly has Windows CRLF line endings, so the value is 5 followed by a carriage return. Command substitution strips the newline but keeps the \r. Check with printf '%q\n' "$n", which shows $'5\r', or with cat -A, which shows 5^M. Strip it with tr -d '\r' when you read the value, or convert the file once with dos2unix or sed -i 's/\r$//'.
How do I compare decimal numbers in bash?
Not with -gt or (( )): bash arithmetic is integer-only and both reject 5.0. Use awk, which compares as floating point and is installed on every Linux system: awk -v a="$n" -v b=3 'BEGIN { exit !(a > b) }' exits 0 when a is greater. bc also works, but it is not in the ubuntu:24.04 container image, where the same line fails with bc: command not found.
Does using [[ ]] fix integer expression expected?
It changes the message, not the bug. [[ 5.0 -gt 3 ]] prints an arithmetic syntax error and returns 1, the same status as an honest false, so an if cannot tell bad input from a smaller number. Worse, [[ abc -gt 3 ]] prints nothing and returns 1, because a bare word is read as a variable name and an unset variable is 0. Validate the value first.
The safe bash script template validates inputs at the top of the script for this reason, and The Production Bash Toolkit ships that template ShellCheck-clean.
Part of the bash snippets collection
Related Scripts
- Fix "unary operator expected" — the same
[ ]test when the variable is empty and unquoted - Fix "bad interpreter: /bin/bash^M" — the same carriage return, in the shebang line
- Bash If Else Examples — every test operator, and which ones compare numbers
- Bash Read File Line by Line — reading values from files without losing or keeping the wrong bytes
- Bash Error Messages, Decoded — the common bash errors, each with the one check that names the cause