← Back to Bash Course | Chapter 3: Control Flow | Lesson 12 of 14

Numeric और String Comparison Operators

Bash numbers compare करने के लिए text compare करने से पूरी तरह अलग operators उपयोग करता है, और उन्हें mix up करना गलत या confusing results देता है।
Syntax
bash
[ "$number1" -lt "$number2" ]
[[ $string1 == $string2 ]]
[[ $string1 < $string2 ]]

Numeric Comparison Operators

Bash के numeric comparison operators -- -eq, -ne, -gt, -ge, -lt, -le -- [ ], [[ ]] के अंदर काम करते हैं, और अपने operands को strictly integers के रूप में treat करते हैं। वे 9 और 10 को numbers के रूप में सही से compare करते हैं, जैसा string comparison नहीं करेगी।

उदाहरण: Numeric Comparison Operators

bash
#!/bin/bash
a=9
b=10
if [ "$a" -lt "$b" ]; then
    echo "$a is numerically less than $b"
fi

String Comparison Operators

String comparison equality के लिए =/== और lexical (dictionary/byte) ordering के लिए </> ([[ ]] के अंदर, या [ ] के अंदर \</\> के रूप में escaped) उपयोग करती है, numeric value के बजाय character by character compare करते हुए।

इसका मतलब है "9" > "10" strings के रूप में true है, क्योंकि 9 1 के बाद sort होता है।

Warning: यह numerically गलत दिखता है लेकिन correct string behavior है -- जब आपका मतलब numeric comparison हो तो -gt/-lt उपयोग करें।

उदाहरण: String Comparison Operators

bash
#!/bin/bash
a="9"
b="10"
if [[ $a > $b ]]; then
    echo "As strings, '$a' sorts after '$b'"
fi

Strings के लिए Leading Zeros और Widths क्यों मायने रखते हैं

क्योंकि string comparison character by character काम करती है, अलग lengths या leading zeros वाली values अपने numeric meaning की तुलना में अप्रत्याशित रूप से sort हो सकती हैं।

Numeric comparison operators दोनों sides को पहले actual integers में convert करके इससे पूरी तरह बचते हैं।

उदाहरण: Why Leading Zeros and Widths Matter for Strings

bash
#!/bin/bash
id1="007"
id2="10"
if [[ $id1 < $id2 ]]; then
    echo "As strings: '$id1' sorts before '$id2'"
fi
if [ "$id1" -lt "$id2" ]; then
    echo "As numbers: $id1 is less than $id2"
fi

एक External Tool से Decimals Compare करना

Bash के -eq/-lt/आदि और (( )) सिर्फ integers support करते हैं, इसलिए 3.5 जैसे decimal numbers compare करने के लिए bc या awk जैसे एक external tool चाहिए, क्योंकि [ 3.5 -gt 2 ] करने की कोशिश एक 'integer expression expected' error produce करती है।

awk आमतौर पर available है और सीधे floating-point comparison perform कर सकता है।

उदाहरण: Comparing Decimals with an External Tool

bash
#!/bin/bash
a="3.5"
b="2"
if awk -v x="$a" -v y="$b" 'BEGIN { exit !(x > y) }'; then
    echo "$a is greater than $b (decimal-aware comparison)"
fi
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. = या == उपयोग करके numbers compare करना जैसे वे -eq के साथ interchangeable हों; "5" = "5.0" false है (अलग text), जबकि 5 -eq 5.0 असल में error देगा क्योंकि arithmetic context को plain integers चाहिए, इसलिए दोनों systems बिलकुल भी interchangeable नहीं हैं।
  2. यह मान लेना कि -gt, -lt, आदि decimal numbers पर काम करते हैं; वे सिर्फ integers handle करते हैं, इसलिए "3.5" -gt "2" compare करना एक error का कारण बनता है, numeric comparison नहीं।
  3. Leading zeros या अलग digit counts वाले numbers पर string comparison operators उपयोग करना, numeric ordering की उम्मीद करते हुए; "9" > "10" एक *string* comparison के रूप में true है (क्योंकि 9 character-by-character 1 के बाद sort होता है) भले ही यह numerically false हो।
चैप्टर सारांश
  • String comparison: =/== (equal), != (not equal), </> (lexical ordering, escaping या [[ ]] चाहिए)।
  • Numeric comparison: -eq, -ne, -gt, -ge, -lt, -le -- सिर्फ integers, decimals नहीं।
  • String </> byte/locale ordering उपयोग करके character by character compare करते हैं, numeric magnitude नहीं।
  • Floating-point comparisons के लिए, Bash का built-in arithmetic काफी नहीं है -- bc या awk जैसे एक external tool उपयोग करें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.