Portable Scripts लिखना (Bash-isms vs POSIX sh)
In this page:
#!/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
#!/usr/bin/env bash
echo "found bash via PATH lookup, running fine"
Login to try C/C++/Java/PHP code in the editor
ध्यान देने वाले Bash-Only Features
Arrays, [[ ]] conditionals, functions के अंदर local variables, और ${var//pattern/repl} जैसे string substitution सभी bash extensions हैं जिन्हें एक strict POSIX sh support नहीं करता।
[[ ]], ${var//a/b}) की हर system पर #!/bin/sh के under काम करने की guarantee नहीं है।उदाहरण: Bash-Only Features to Watch For
#!/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//-/ }"
Login to try C/C++/Java/PHP code in the editor
POSIX-Compatible Equivalent लिखना
जब plain sh के लिए portability मायने रखती है, वही logic आमतौर पर single-bracket [ ] tests, case statements, और plain variables से express की जा सकती है arrays के बजाय, जो सभी POSIX standard का हिस्सा हैं।
उदाहरण: Writing the POSIX-Compatible Equivalent
#!/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
Login to try C/C++/Java/PHP code in the editor
#!/bin/shको shebang के रूप में उपयोग करना जबकि अभी भी arrays या[[ ]]जैसे bash-only features पर rely करना, जो उन systems पर fail होता है जहां/bin/shएक अलग, stricter shell है।- यह मान लेना कि
[[ ]], arrays,local, और+=हर जगह available हैं; वे bash extensions हैं, POSIXshका हिस्सा नहीं। #!/bin/bashhardcode करना और मान लेना कि bash हमेशा उस exact path पर है;#!/usr/bin/env bashउन systems में ज़्यादा portable है जहां bash कहीं और रहता है।
#!/usr/bin/env bashbash को PATH पर जहां भी हो ढूंढता है, जो#!/bin/bashhardcode करने से ज़्यादा portable है।- Arrays,
[[ ]],local,${var//a/b}जैसे string manipulation, और namerefs bash-isms हैं जो POSIXshमें guaranteed नहीं हैं। - अगर एक script को plain
shके under चलना है,[ ](single bracket) से chipके रहें, arrays avoid करें, और bash-only pattern matching के बजायcaseउपयोग करें। - सही
--shelltarget के साथshellcheckrun करना एक POSIX-compliant intended script में accidental bash-isms पकड़ने में मदद करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- Quoting Pitfalls and Why to Always Quote Variables
- set -euo pipefail as a Script Header
- Structuring a Script (Functions, main() Pattern, Exit at the End)
- Idempotent Scripts (Safe to Re-Run)
- Logging in Scripts (Timestamped log Function)
- Writing Portable Scripts (Bash-isms vs POSIX sh)
- Capstone: A Real-World Backup/Cleanup Script