← Back to Bash Course | Chapter 12: Error Handling & Debugging | Lesson 11 of 12

Portability क्या है

Portability अपने किसी classmate को सरल शब्दों में एक note लिखने जैसा है जो हर कोई पढ़ सके, एक secret slang के बजाय जिसे सिर्फ आपका best friend समझे। Simple scripts ज़्यादा computers पर चलती हैं।

In this page:

  1. Portability
Syntax
bash
#!/bin/sh
# POSIX-only features, no Bash extensions
if [ condition ]; then
  command
fi

Portability

Different systems पर चलने वाली Scripts को Bash-only features (जैसे arrays या [[ ]]) से बचना चाहिए अगर उन्हें plain POSIX sh के अंदर काम करना हो, और macOS/BSD tools पर अलग GNU-specific flags मान लेने से बचना चाहिए।

Actual target systems पर testing करना, और hardcoded path के बजाय #!/usr/bin/env bash उपयोग करना, दोनों portability improve करते हैं। एक script किस shell और tool versions पर depend करती है इसके बारे में explicit होना "works on my machine" surprises रोकता है।

Note: अगर एक script सिर्फ POSIX features उपयोग करती है, इसे एक .sh extension और #!/bin/sh shebang दें यह signal करने के लिए कि इसे specifically Bash की ज़रूरत नहीं।

उदाहरण: Portability

bash
#!/usr/bin/env bash
# Using env in the shebang finds bash wherever it's installed,
# which is more portable across different systems.
echo "Bash version: $BASH_VERSION"
echo "This script relies on Bash-specific features like arrays:"
arr=(1 2 3)
echo "${arr[@]}"
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 के साथ [[ ]] या arrays जैसे Bash-only features उपयोग करना।
  2. यह मान लेना कि commands और options Linux और macOS में same हैं।
  3. bash का path hardcode करना, जब #!/usr/bin/env bash ज़्यादा 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.