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

तार्किक Operators

&&, ||, और ! आपको conditions combine या invert करने देते हैं ताकि एक command सिर्फ तब चले जब certain success/failure combinations हों।
Syntax
bash
command1 && command2   # run command2 if command1 succeeds
command1 || command2   # run command2 if command1 fails
! command

Sequential Success के लिए &&

&& अपने right वाले command को सिर्फ तब चलाता है जब इसके left वाला command status 0 के साथ exit हुआ हो। यह उन steps को chain करने का एक compact तरीका है जो सिर्फ तब आगे बढ़ने चाहिए जब पिछला काम कर गया हो, एक पूरा if block लिखे बिना।

उदाहरण: && for Sequential Success

bash
#!/bin/bash
mkdir -p output_dir && echo "Directory ready, continuing"
cd output_dir && echo "Now inside output_dir: $(pwd)"

Failure पर Fallback के लिए ||

|| अपने right वाले command को सिर्फ तब चलाता है जब इसके left वाला command एक non-zero status के साथ exit हुआ हो। यह आमतौर पर एक fallback action या error message देने के लिए उपयोग किया जाता है जब कुछ fail हो।

उदाहरण: || for Fallback on Failure

bash
#!/bin/bash
grep -q "xyz" <<< "abc def" || echo "Pattern 'xyz' was not found, using fallback"

एक Condition को Negate करने के लिए !

! इसके ठीक बाद आने वाले command या test का exit status invert करता है, success को failure में और failure को success में बदलते हुए। [ ] या [[ ]] के अंदर, इसे negate की जा रही condition के ठीक पहले रखा जाता है।

उदाहरण: ! to Negate a Condition

bash
#!/bin/bash
if [ ! -f nonexistent_file.txt ]; then
    echo "Confirmed: nonexistent_file.txt does not exist"
fi
if ! grep -q "zzz" <<< "hello world"; then
    echo "Confirmed: pattern not found"
fi

&& और || को साथ Combine करना

&& और || को एक line पर एक compact 'if success do X, else do Y' pattern के लिए chain किया जा सकता है, strictly left से right evaluated होते हुए। ज़्यादा complex combinations के साथ careful रहें -- एक simple two-way branch से आगे किसी भी चीज़ के लिए, एक proper if/else clearer और less error-prone है।

Warning: अगर एक &&/|| chain में middle command खुद उन कारणों से fail हो सकता है जो condition से unrelated हैं, तो || fallback अप्रत्याशित रूप से चल सकता है -- जब logic complex हो जाए तो if/else prefer करें।

उदाहरण: Combining && and || Together

bash
#!/bin/bash
value=10
[ "$value" -gt 5 ] && echo "Value is greater than 5" || echo "Value is 5 or less"
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. यह सोचना कि &&/|| true/false booleans पर operate करते हैं; वे असल में एक command के exit status (0 = success) के आधार पर chain करते हैं, इसलिए command1 && command2 का असल मतलब है 'command2 सिर्फ तब चलाएं अगर command1 0 exited हुआ'।
  2. [ ] बनाम [[ ]] के अंदर negate करते समय ! को गलत जगह रखना; ! को expression के ठीक पहले जाना चाहिए ([ ! -f file ]), पूरे if के पहले नहीं।
  3. बिना parentheses/grouping के कई &&/|| को साथ chain करना और result से surprised होना, क्योंकि && और || equal precedence के साथ left से right evaluated होते हैं, कुछ दूसरी languages के विपरीत।
चैप्टर सारांश
  • cmd1 && cmd2 cmd2 को सिर्फ तब चलाता है जब cmd1 status 0 (succeeded) के साथ exit हुआ हो।
  • cmd1 || cmd2 cmd2 को सिर्फ तब चलाता है जब cmd1 एक non-zero status (failed) के साथ exit हुआ हो।
  • ! इसके बाद आने वाले command या test का exit status negate करता है -- success failure बन जाता है और vice versa।
  • [[ ]] के अंदर, && और || sub-conditions को सीधे combine करते हैं; [[ ]]/(( )) के बाहर, वे पूरे commands combine करते हैं।

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.