← Back to Bash Course | Chapter 7: String Manipulation | Lesson 2 of 10

test कमांड

test command computer के लिए एक yes-or-no quiz question जैसा है, जैसे 'क्या यह file वहां है?'। यह quietly pass या fail कहकर जवाब देता है, और script उस आधार पर decide करती है कि क्या करना है।

In this page:

  1. The test Command
Syntax
bash
test expression
[ expression ]

The test Command

test expression (जो [ expression ] के रूप में भी लिखा जाता है) एक condition evaluate करता है और इसके अनुसार इसका exit status set करता है, conditions लिखने का classic POSIX तरीका बनाते हुए।

[ ] को हर token के आस-पास और brackets खुद के आस-पास spaces चाहिए। यह ज़्यादा feature-rich [[ ]] से पहले आता है और POSIX-portable scripts के लिए अब भी उपयोग होता है।

Warning: एक space missing होना, जैसे [$a -eq $b], एक syntax error है — [ असल में एक command है जिसे argument separators के रूप में spaces चाहिए।

उदाहरण: The test Command

bash
#!/bin/bash
a=5

if test "$a" -eq 5; then
    echo "a equals 5 (using test)"
fi

if [ "$a" -eq 5 ]; then
    echo "a equals 5 (using [ ])"
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. Brackets के आस-पास spaces भूल जाना, जैसे [$a -eq 1], जो एक error है।
  2. Tests में variables को unquoted छोड़ना, इसलिए एक empty value unary operator expected देती है।
  3. [ ] में == और = को inconsistently उपयोग करना, क्योंकि = portable वाला है।

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.