Quoting की गलतियाँ और Variables को हमेशा Quote क्यों करें
In this page:
"$variable_name"
"${array_name[@]}"
Unquoted Variables पर Word Splitting
जब spaces वाला एक variable unquoted उपयोग होता है, bash इसे एक command को pass करने से पहले IFS पर कई separate words में split कर देता है, जो आमतौर पर intended नहीं था।
उदाहरण: Word Splitting on Unquoted Variables
#!/bin/bash
count_args() { echo "received $# argument(s)"; }
phrase="hello there world"
count_args $phrase
count_args "$phrase"
Login to try C/C++/Java/PHP code in the editor
Accidental Globbing
* जैसे glob characters वाली value वाला एक unquoted variable current directory में असली filenames के against expand हो जाता है, जो silently एक literal string को unrelated filenames की एक list में बदल सकता है।
उदाहरण: Accidental Globbing
#!/bin/bash
touch star_demo_a.txt star_demo_b.txt
pattern="star_demo_*.txt"
echo "unquoted (expands against real files): $pattern"
echo unquoted: $pattern
echo "quoted (kept literal): \"$pattern\""
rm -f star_demo_a.txt star_demo_b.txt
Login to try C/C++/Java/PHP code in the editor
गायब होती Empty Variables
एक unquoted empty (या unset) variable कुछ भी नहीं तक expand होता है, command line से effectively vanish होते हुए, जो अगला असली argument कैसे interpreted होता है इसे shift कर सकता है। Quoting इसे एक genuine empty-string argument रखता है।
उदाहरण: Disappearing Empty Variables
#!/bin/bash
show_args() {
echo "count: $#"
for a in "$@"; do echo "[${a}]"; done
}
empty_var=""
show_args $empty_var second
echo "---"
show_args "$empty_var" second
Login to try C/C++/Java/PHP code in the editor
एक Safe Default Habit
By default हर variable expansion को quote करना, और सिर्फ जब word splitting या globbing actually चाहिए हो तब deliberately एक unquoted छोड़ना, subtle bugs की एक पूरी category से बचाता है।
shellcheck जैसे tools ज़्यादातर unquoted-variable mistakes अपने आप flag करते हैं और किसी भी real script पर run करने के लायक हैं।उदाहरण: A Safe Default Habit
#!/bin/bash
filename="my report.txt"
touch "$filename"
if [[ -f "$filename" ]]; then
echo "file with a space in its name handled correctly"
fi
rm -f "$filename"
Login to try C/C++/Java/PHP code in the editor
- एक variable को unquoted छोड़ना और यह मान लेना कि यह हमेशा एक value की तरह treat होगा, जब Bash असल में इसे पहले whitespace पर word-split कर देता है।
- यह भूल जाना कि
*या?वाला एक unquoted variable current directory में असली filenames के against glob-expand होता है। - यह believe करना कि quoting सिर्फ उन values के लिए चाहिए जिनमें "शायद spaces हों"; एक empty unquoted variable भी पूरी तरह vanish हो सकता है और positional arguments shift कर सकता है।
- Unquoted variable expansions command run होने से पहले word splitting (IFS पर) और filename globbing से गुजरते हैं।
"$var"दोनों word splitting और globbing रोकता है, variable की exact value को एक argument के रूप में preserve करते हुए।- एक unquoted empty variable पूरी तरह vanish हो जाता है एक empty argument बनने के बजाय, जो positional arguments को unexpectedly shift कर सकता है।
- एक default habit के रूप में, हर variable expansion को quote करें जब तक आपको specifically word splitting या globbing न चाहिए हो।
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