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

Quoting की गलतियाँ और Variables को हमेशा Quote क्यों करें

Bash में एक variable के आस-पास quotation marks लगाना इसकी value को accidentally pieces में split होने या एक wildcard pattern की तरह treat होने से protect करता है।
Syntax
bash
"$variable_name"
"${array_name[@]}"

Unquoted Variables पर Word Splitting

जब spaces वाला एक variable unquoted उपयोग होता है, bash इसे एक command को pass करने से पहले IFS पर कई separate words में split कर देता है, जो आमतौर पर intended नहीं था।

उदाहरण: Word Splitting on Unquoted Variables

bash
#!/bin/bash
count_args() { echo "received $# argument(s)"; }

phrase="hello there world"
count_args $phrase
count_args "$phrase"

Accidental Globbing

* जैसे glob characters वाली value वाला एक unquoted variable current directory में असली filenames के against expand हो जाता है, जो silently एक literal string को unrelated filenames की एक list में बदल सकता है।

उदाहरण: Accidental Globbing

bash
#!/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

गायब होती Empty Variables

एक unquoted empty (या unset) variable कुछ भी नहीं तक expand होता है, command line से effectively vanish होते हुए, जो अगला असली argument कैसे interpreted होता है इसे shift कर सकता है। Quoting इसे एक genuine empty-string argument रखता है।

उदाहरण: Disappearing Empty Variables

bash
#!/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

एक Safe Default Habit

By default हर variable expansion को quote करना, और सिर्फ जब word splitting या globbing actually चाहिए हो तब deliberately एक unquoted छोड़ना, subtle bugs की एक पूरी category से बचाता है।

Note: shellcheck जैसे tools ज़्यादातर unquoted-variable mistakes अपने आप flag करते हैं और किसी भी real script पर run करने के लायक हैं।

उदाहरण: A Safe Default Habit

bash
#!/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"
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. एक variable को unquoted छोड़ना और यह मान लेना कि यह हमेशा एक value की तरह treat होगा, जब Bash असल में इसे पहले whitespace पर word-split कर देता है।
  2. यह भूल जाना कि * या ? वाला एक unquoted variable current directory में असली filenames के against glob-expand होता है।
  3. यह 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 न चाहिए हो।

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.