← Back to Bash Course | Chapter 2: Variables & Types | Lesson 2 of 13

String Variables और Quoting के नियम

Quotes control करते हैं कि Bash text को literally treat करे या इसके अंदर variables और दूसरे special expansions ढूंढे।
Syntax
bash
variable_name="value with spaces"
echo '$literal_text'
echo "expanded: $variable_name"

Single बनाम Double Quotes

Single quotes text को पूरी तरह literally preserve करते हैं, इसलिए single quotes के अंदर $name literal characters $name के रूप में print होता है, expand होने के बजाय। Double quotes अब भी variable expansion, command substitution, और arithmetic expansion perform करते हैं, जबकि result को word splitting और globbing से protect करते हुए।

उदाहरण: Single vs Double Quotes

bash
#!/bin/bash
name="Ada"
echo 'Hello, $name'
echo "Hello, $name"

Variables Quote करना क्यों मायने रखता है

एक variable को unquoted छोड़ना Bash को इस पर word splitting (value को spaces/tabs/newlines पर तोड़ना) और pathname expansion apply करने देता है इससे पहले कि command इसे देखे।

Variable को double quotes से quote करना इसकी value को एक single argument के रूप में intact रखता है, जो almost हमेशा वही है जो आप चाहते हैं।

Warning: Unquoted $sentence multiple spaces को single spaces में collapse करता है क्योंकि word splitting extra whitespace discard करता है; quoted "$sentence" इसे exactly preserve करता है।

उदाहरण: Why Quoting Variables Matters

bash
#!/bin/bash
sentence="multiple   words   here"
echo $sentence
echo "---"
echo "$sentence"

एक Single Quote को Escape करना

क्योंकि single quotes के अंदर कुछ भी escape नहीं किया जा सकता, एक literal single quote embed करने के लिए quoted string को बंद करना, quote character को किसी और तरीके से insert करना, और quoted string को फिर से खोलना ज़रूरी है।

Common idiom '...'\''...' है, जो बंद करता है, एक quote escape करता है, फिर reopen करता है।

उदाहरण: Escaping a Single Quote

bash
#!/bin/bash
message='It'\''s a sunny day'
echo "$message"

Quotes और Expansions को Mix करना

एक single double-quoted string के अंदर literal text, variables, और command substitution combine करना आम है, क्योंकि double quotes Bash के सभी expansions allow करते हैं जबकि पूरी चीज़ को एक argument के रूप में treat करते हैं।

यह बहुत सारे अलग echo calls को juggle किए बिना output को readable रखता है।

उदाहरण: Mixing Quotes and Expansions

bash
#!/bin/bash
user="ada"
host=$(hostname)
echo "User $user is connected to $host at $(date +%H:%M:%S)"
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. यह सोचना कि single और double quotes interchangeable हैं; single quotes सभी expansion ($variable सहित) suppress करते हैं, जबकि double quotes अब भी variable और command substitution allow करते हैं।
  2. एक variable को unquoted छोड़ना जब इसमें spaces हो सकते हैं, जैसे echo $name की बजाय echo "$name", जो word splitting cause करता है और value में spaces या glob characters होने पर command को तोड़ सकता है।
  3. एक single-quoted string के अंदर एक single quote को backslash से escape करने की कोशिश करना; वह Bash में काम नहीं करता -- आपको quote बंद करना, एक escaped या double-quoted quote insert करना, फिर इसे reopen करना होता है।
चैप्टर सारांश
  • Single quotes '...' हर character को literally preserve करते हैं -- कोई variable expansion नहीं, कोई command substitution नहीं।
  • Double quotes "..." अब भी variables ($var), command substitution ($(...)), और arithmetic ($(( ))) expand करते हैं, लेकिन result पर glob expansion और word splitting suppress करते हैं।
  • Variable references को हमेशा double-quote करें ("$var") जब तक आप specifically word splitting/globbing होना न चाहें।
  • Unquoted strings word splitting और pathname expansion (globbing) के subject हैं, जो variables के लिए शायद ही कभी वह है जो आप चाहते हैं।

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.