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

test और [ ] Operators

[ ] (और equivalent test command) conditions जांचने का classic, POSIX-standard तरीका है, जैसे दो strings बराबर हैं या एक number दूसरे से बड़ा है।
Syntax
bash
[ "$value1" -eq "$value2" ]
[ "$string1" = "$string2" ]
test expression

[ ] असल में test Command है

[ expression ] literally test command को एक call है, जहां [ एक program/builtin है जिसे अपने final argument के रूप में एक matching ] चाहिए।

यही कारण है कि दोनों brackets के आसपास spaces mandatory हैं -- वे separate arguments हैं, decorative syntax नहीं।

उदाहरण: [ ] Is the test Command

bash
#!/bin/bash
if test 5 -eq 5; then
    echo "test command: equal"
fi
if [ 5 -eq 5 ]; then
    echo "[ ] form: equal"
fi

Numeric Comparisons

[ ] के अंदर, numeric comparisons named operators उपयोग करते हैं: -eq (equal), -ne (not equal), -gt (greater than), -ge (greater or equal), -lt (less than), और -le (less or equal)।

यहां इसके बजाय > या < उपयोग करना shell redirection perform करता है, comparison नहीं, जो एक common mistake है।

उदाहरण: Numeric Comparisons

bash
#!/bin/bash
a=10
b=20
if [ "$a" -lt "$b" ]; then
    echo "$a is less than $b"
fi
if [ "$a" -ne "$b" ]; then
    echo "$a is not equal to $b"
fi

String Comparisons

[ ] के अंदर, string equality एक single = उपयोग करती है (हालांकि == भी एक Bash extension के रूप में काम करता है) और inequality != उपयोग करती है। String comparisons character-by-character equality जांचते हैं, numeric value नहीं, इसलिए "5" = "05" false होगा भले ही वे same number represent करते हों।

उदाहरण: String Comparisons

bash
#!/bin/bash
name="ada"
if [ "$name" = "ada" ]; then
    echo "Name matches"
fi
if [ "$name" != "grace" ]; then
    echo "Name is not grace"
fi

File Test Operators

[ ] file test operators भी support करता है जैसे -e (path exists), -f (is a regular file), -d (is a directory), और -r/-w/-x (readable/writable/executable)। ये उन scripts के लिए essential हैं जो filesystem पर act करने से पहले इसे जांचते हैं।

उदाहरण: File Test Operators

bash
#!/bin/bash
touch sample.txt
if [ -e sample.txt ]; then
    echo "sample.txt exists"
fi
if [ -f sample.txt ]; then
    echo "sample.txt is a regular file"
fi
if [ ! -d sample.txt ]; then
    echo "sample.txt is not a directory"
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. [ और ] के आसपास required spaces भूल जाना; [$x -eq 5] एक syntax error है, यह [ $x -eq 5 ] दोनों तरफ spaces के साथ होना चाहिए।
  2. [ ] के अंदर numeric comparisons के लिए == या >/< उपयोग करना; वहां वे string operators हैं (और >/< को redirection के रूप में treat होने से बचने के लिए escaping चाहिए) -- numeric comparison को -eq, -gt, -lt, आदि चाहिए।
  3. [ ] के अंदर variables को quote न करना, जैसे [ $name = "ada" ]; अगर $name empty है या इसमें spaces हैं, तो यह एक false condition के बजाय एक syntax error में बदल सकता है।
चैप्टर सारांश
  • [ ] literally test command है; [ expr ] और test expr equivalent हैं, [ बस एक alias है जिसे एक matching ] चाहिए।
  • String comparisons = और != उपयोग करती हैं; numeric comparisons -eq, -ne, -gt, -ge, -lt, -le उपयोग करती हैं।
  • -f, -d, -e जैसे file tests जांचते हैं कि एक path एक regular file है, एक directory है, या बिलकुल exist करता है।
  • [ ] के अंदर हमेशा variables को quote करें, जैसे [ "$var" = "value" ], errors से बचने के लिए जब variable empty हो या इसमें spaces हों।

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.