From 86c41d5cb27d8ea185212c2dc3767fa779633b59 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Mon, 30 Aug 2021 20:01:27 -0700 Subject: [PATCH] Updated SC2170 (markdown) --- SC2170.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/SC2170.md b/SC2170.md index 62fc72e..96f9e0b 100644 --- a/SC2170.md +++ b/SC2170.md @@ -31,13 +31,11 @@ fi ``` ### Rationale: -You are comparing a string value with a numerical operator, such as `-eq`, `-ne`, `-lt` or `-gt`. +You are comparing a string value with a numerical operator, such as `-eq`, `-ne`, `-lt` or `-gt`. These only work for numbers. -In `[[ .. ]]`, this would automatically dereference the string, looking to see if there are variables by that name. +If you want to compare the value as a string, switch to the equivalent string operator: `=`, `!=` `\<` or `\>`. -In `[ .. ]`, which you are using, the string is just treated as an invalid number. - -If you want to compare numbers, expand yourself (e.g. use `$var` instead of `var`, or `$((n+1))` instead of `n+1`). If you are trying to compare strings and not numbers, use `=`, `!=` `\<` or `\>` instead. +If you want to compare it as a number, such as `n=42; while [ n -gt 1024/8 ]; ..`, then keep the operator and expand the operands yourself with `$var` or `$((expr))`: `while [ "$n" -gt $((1024/8)) ]` ### Exceptions: