तुलना Operators
Comparison operators एक balance scale जैसे हैं जो check करता है कि एक number दूसरे से bigger, smaller या same है या नहीं। Bash में square brackets के लिए scale words का एक set है और double parentheses के लिए दूसरा।
In this page:
Syntax
[[ $a -eq $b ]] # also -ne -lt -le -gt -ge
(( a == b )) # also != < <= > >=
Comparison Operators
[[ ]] या (( )) के अंदर, numeric comparisons -eq -ne -lt -le -gt -ge ([[ ]] में) या familiar == != < <= > >= ((( )) में) उपयोग करते हैं।
Ambiguity से बचने के लिए Bash numeric comparison syntax को string comparison से अलग रखता है। -eq (numeric) को गलत context में == के साथ mix up करना एक आम beginner mistake है।
Warning:
[ ] (single brackets) के अंदर == उपयोग करना strings के लिए काम करता है, लेकिन numbers के लिए आपको -eq उपयोग करना चाहिए, == नहीं।उदाहरण: Comparison Operators
#!/bin/bash
a=5
b=8
if [ "$a" -lt "$b" ]; then
echo "$a is less than $b"
fi
if (( a < b )); then
echo "Using (( )): $a is less than $b"
fi
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- Numbers के लिए
[ ]के अंदर>और<उपयोग करना, जिन्हें redirections माना जाता है;-gtऔर-ltउपयोग करें। - Strings पर
-eqउपयोग करना, जोinteger expression expectedदेता है। - Brackets के अंदर spaces भूल जाना, जैसे
[$a -eq 1]।
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: