← Back to Bash Course | Chapter 14: Best Practices & Real-World Scripting | Lesson 6 of 7

Portable Scripts लिखना (Bash-isms vs POSIX sh)

कुछ Bash features सिर्फ Bash में काम करते हैं, इसलिए एक script जिसे ज़्यादा minimal shells पर चलना है उन्हें avoid करना पड़ता है और commands के एक छोटे, ज़्यादा universal set से chipके रहना पड़ता है।
Syntax
bash
#!/usr/bin/env bash
# Bash-only
if [[ condition ]]; then
  command
fi

#!/bin/sh
# POSIX equivalent
if [ condition ]; then
  command
fi

सही Shebang चुनना

#!/usr/bin/env bash current PATH पर bash ढूंढता है यह मान लेने के बजाय कि यह /bin/bash पर रहता है, जो एक script को उन systems (जैसे कुछ BSDs या containers) में ज़्यादा portable बनाता है जहां bash कहीं और installed है।

उदाहरण: Choosing the Right Shebang

bash
#!/usr/bin/env bash
echo "found bash via PATH lookup, running fine"

ध्यान देने वाले Bash-Only Features

Arrays, [[ ]] conditionals, functions के अंदर local variables, और ${var//pattern/repl} जैसे string substitution सभी bash extensions हैं जिन्हें एक strict POSIX sh support नहीं करता।

Warning: इस example के किसी भी feature (arrays, [[ ]], ${var//a/b}) की हर system पर #!/bin/sh के under काम करने की guarantee नहीं है।

उदाहरण: Bash-Only Features to Watch For

bash
#!/bin/bash
fruits=("apple" "banana")
if [[ ${#fruits[@]} -eq 2 ]]; then
    echo "arrays and [[ ]] are bash-specific features working here"
fi

text="hello-world"
echo "${text//-/ }"

POSIX-Compatible Equivalent लिखना

जब plain sh के लिए portability मायने रखती है, वही logic आमतौर पर single-bracket [ ] tests, case statements, और plain variables से express की जा सकती है arrays के बजाय, जो सभी POSIX standard का हिस्सा हैं।

उदाहरण: Writing the POSIX-Compatible Equivalent

bash
#!/bin/bash
value="banana"
case $value in
    apple) echo "it's an apple" ;;
    banana) echo "it's a banana (POSIX-compatible case statement)" ;;
    *) echo "unknown fruit" ;;
esac

if [ "$value" = "banana" ]; then
    echo "single-bracket test also works everywhere"
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. #!/bin/sh को shebang के रूप में उपयोग करना जबकि अभी भी arrays या [[ ]] जैसे bash-only features पर rely करना, जो उन systems पर fail होता है जहां /bin/sh एक अलग, stricter shell है।
  2. यह मान लेना कि [[ ]], arrays, local, और += हर जगह available हैं; वे bash extensions हैं, POSIX sh का हिस्सा नहीं।
  3. #!/bin/bash hardcode करना और मान लेना कि bash हमेशा उस exact path पर है; #!/usr/bin/env bash उन systems में ज़्यादा portable है जहां bash कहीं और रहता है।
चैप्टर सारांश
  • #!/usr/bin/env bash bash को PATH पर जहां भी हो ढूंढता है, जो #!/bin/bash hardcode करने से ज़्यादा portable है।
  • Arrays, [[ ]], local, ${var//a/b} जैसे string manipulation, और namerefs bash-isms हैं जो POSIX sh में guaranteed नहीं हैं।
  • अगर एक script को plain sh के under चलना है, [ ] (single bracket) से chipके रहें, arrays avoid करें, और bash-only pattern matching के बजाय case उपयोग करें।
  • सही --shell target के साथ shellcheck run करना एक POSIX-compliant intended script में accidental bash-isms पकड़ने में मदद करता है।

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.