Portability क्या है
Portability अपने किसी classmate को सरल शब्दों में एक note लिखने जैसा है जो हर कोई पढ़ सके, एक secret slang के बजाय जिसे सिर्फ आपका best friend समझे। Simple scripts ज़्यादा computers पर चलती हैं।
In this page:
Syntax
#!/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
#!/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[@]}"
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
#!/bin/shके साथ[[ ]]या arrays जैसे Bash-only features उपयोग करना।- यह मान लेना कि commands और options Linux और macOS में same हैं।
bashका path hardcode करना, जब#!/usr/bin/env bashज़्यादा portable है।
🔒
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Exit Codes and Checking $? Correctly
- set -e and set -u
- Error Handling
- set -e (Exit on Error) and Its Gotchas
- Logging
- set -u (Undefined Variables) and set -x (Trace Mode)
- Script Arguments with getopts
- trap ERR for Custom Error Handling
- Debug Mode (set -x)
- Writing a Custom Error/die Function
- Portability
- Validating Script Input and Arguments