Skip to content

Files

Latest commit

 

History

History
27 lines (17 loc) · 820 Bytes

SC2309.md

File metadata and controls

27 lines (17 loc) · 820 Bytes

Pattern: Use of string as argument to numerical operator

Issue: -

Description

ShellCheck found a string used as an argument to a numerical operator like -eq, -ne, -lt, -ge. Such strings will be treated as arithmetic expressions, meaning n will refer to a variable $n, and 24/12 will be evaluated into 2.

In the problematic example, the intention was instead to compare "n" as a string, so it should use the equivalent string operator instead, in this case =.

Example of incorrect code:

read -p "Continue? [y/n] " var
[ "$var" -eq "n" ​] && exit 1

Example of correct code:

#read -p "Continue? [y/n] " var
[ "$var" = "n" ​] && exit 1

Further Reading