← Back to Bash Course | Chapter 4: Loops | Lesson 9 of 14

तुलना Operators

Comparison operators एक balance scale जैसे हैं जो check करता है कि एक number दूसरे से bigger, smaller या same है या नहीं। Bash में square brackets के लिए scale words का एक set है और double parentheses के लिए दूसरा।

In this page:

  1. Comparison Operators
Syntax
bash
[[ $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

bash
#!/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
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 के लिए [ ] के अंदर > और < उपयोग करना, जिन्हें redirections माना जाता है; -gt और -lt उपयोग करें।
  2. Strings पर -eq उपयोग करना, जो integer expression expected देता है।
  3. Brackets के अंदर spaces भूल जाना, जैसे [$a -eq 1]।

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.