Numeric और String Comparison Operators
In this page:
[ "$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
#!/bin/bash
a=9
b=10
if [ "$a" -lt "$b" ]; then
echo "$a is numerically less than $b"
fi
Login to try C/C++/Java/PHP code in the editor
String Comparison Operators
String comparison equality के लिए =/== और lexical (dictionary/byte) ordering के लिए </> ([[ ]] के अंदर, या [ ] के अंदर \</\> के रूप में escaped) उपयोग करती है, numeric value के बजाय character by character compare करते हुए।
इसका मतलब है "9" > "10" strings के रूप में true है, क्योंकि 9 1 के बाद sort होता है।
-gt/-lt उपयोग करें।उदाहरण: String Comparison Operators
#!/bin/bash
a="9"
b="10"
if [[ $a > $b ]]; then
echo "As strings, '$a' sorts after '$b'"
fi
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
एक 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
#!/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
Login to try C/C++/Java/PHP code in the editor
=या==उपयोग करके numbers compare करना जैसे वे-eqके साथ interchangeable हों;"5" = "5.0"false है (अलग text), जबकि5 -eq 5.0असल में error देगा क्योंकि arithmetic context को plain integers चाहिए, इसलिए दोनों systems बिलकुल भी interchangeable नहीं हैं।- यह मान लेना कि
-gt,-lt, आदि decimal numbers पर काम करते हैं; वे सिर्फ integers handle करते हैं, इसलिए"3.5" -gt "2"compare करना एक error का कारण बनता है, numeric comparison नहीं। - 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 उपयोग करें।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: