Skip to content

Fix "integer expression expected" in Bash: CRLF, Floats and Empty Values

troubleshootingtestcrlfarithmeticerror-handling
7 min read

Quick Answer

The bash error [: 5.0: integer expression expected means test ([ ]) got a value for -eq, -ne, -lt, -le, -gt or -ge that is not a whole number. The text between the colons is the bad value: empty ("[: : integer expression expected"), a decimal such as 5.0, a word, or a number with a hidden carriage return from a file saved on Windows. The test exits 2, so an if takes its else branch. Bash 5.3 shortened the message to integer expected, so search for both. Fix it at the source. Strip carriage returns with tr -d '\r' or ${n%$'\r'}. Validate before comparing with [[ $n =~ ^[0-9]+$ ]]. Give an empty value a default with ${n:-0}. Compare decimals with awk, because bash arithmetic is integer-only. Switching to [[ ]] or (( )) does not fix bad input: those print an arithmetic syntax error and return 1, which looks the same as false.

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:

text
$ printf 'max_jobs=5\r\nmin_free=10\r\n' > settings.conf $ file settings.conf settings.conf: ASCII text, with CRLF line terminators $ cat crlf.sh n=$(cut -d= -f2 settings.conf | head -1) printf 'n=%q\n' "$n" [ "$n" -gt 3 ] && echo bigger; echo "exit=$?"

Bash 5.2.21 (docker run --rm ubuntu:24.04), output piped through cat -A so the carriage return shows as ^M:

text
n=$'5\r' crlf.sh: line 4: [: 5^M: integer expression expected exit=2

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:

text
n=$'5\r' bigger exit=0

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 2true, exit 0invalid arithmetic operator (error token is "^M"), exit 1
''[: : integer expression expected, exit 2[: : integer expected, exit 2no message, exit 1
5.0[: 5.0: integer expression expected, exit 2[: 5.0: integer expected, exit 2invalid arithmetic operator (error token is ".0"), exit 1
abc[: abc: integer expression expected, exit 2[: abc: integer expected, exit 2no message, exit 1
" 5", "5 "true, exit 0true, exit 0true, exit 0

The raw bash 5.3.15 run behind the 5.3 column and the [[ ]] column:

text
n=$'5\r' [ ] bigger [ ] exit=0 cases.sh: line 6: [[: 5^M: arithmetic syntax error: invalid arithmetic operator (error token is "^M") [[ ]] exit=1 cases.sh: line 7: ((: 5^M: arithmetic syntax error: invalid arithmetic operator (error token is "^M") (( )) exit=1 n='' cases.sh: line 5: [: : integer expected [ ] exit=2 [[ ]] exit=1 (( )) exit=1 n=5.0 cases.sh: line 5: [: 5.0: integer expected [ ] exit=2 cases.sh: line 6: [[: 5.0: arithmetic syntax error: invalid arithmetic operator (error token is ".0") [[ ]] exit=1 n=abc cases.sh: line 5: [: abc: integer expected [ ] exit=2 [[ ]] exit=1 (( )) exit=1

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):

text
dash: 1: [: Illegal number: 5.0 # dash 0.5.12, /bin/sh on Debian and Ubuntu test: invalid integer '5.0' # coreutils /usr/bin/test 9.10 sh: 5.0: bad number # busybox 1.37

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.

bash
n=$(cut -d= -f2 settings.conf | head -1 | tr -d '\r') # every CR, at the source n=${n%$'\r'} # or one trailing CR, no subprocess
text
n=5 bigger exit=0

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.

bash
if [[ $n =~ ^[0-9]+$ ]]; then [ "$n" -gt 3 ] && echo bigger else echo "not a whole number: $(printf '%q' "$n")" >&2 exit 1 fi
text
5: integer '': rejected 5.0: rejected abc: rejected \ 5: rejected -3: rejected 007: integer 08: integer

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:

text
$'5\r' -> 5 5.0 -> 50 \ 5 -> 5 -3 -> 3 1\,200 -> 1200

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 (( )).

bash
awk -v a="$n" -v b="$t" 'BEGIN { exit !(a > b) }' && echo "$n > $t"
text
awk: 5.0 > 3 awk exit=0 awk exit=1 # n=2.75, t=3

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:

text
$ shellcheck sc.sh In sc.sh line 3: if [ "$n" -gt 3.5 ]; then echo big; fi ^-^ SC2072 (error): Decimals are not supported. Either use integers only, or use bc or awk to compare. In sc.sh line 4: if [ "$n" > 3 ]; then echo big; fi ^-- SC2071 (error): > is for string comparisons. Use -gt instead.

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

PAID RESOURCE — $9

The Production Bash Toolkit

An operational script system + a 30-function shared library + a 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →
curl -O bashlib-starter.sh

Get the bashlib starter

Ten functions I source into every script on my own boxes — strict-mode setup, an ERR trap that names the failing line, lock and timeout wrappers, and cleanup that runs on every exit path. After that, a short email only when something breaks on a real box and I've worked out why, about twice a month. One-click unsubscribe.

BashSnippets logo

Written by Travis

Creator of BashSnippets.xyz

bashsnippets.xyz/about

Related Snippets

Frequently Asked Questions

faq — snippet

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.

faq — snippet

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.

faq — snippet

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$//'.

faq — snippet

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.

faq — snippet

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.