String Variables और Quoting के नियम
In this page:
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
#!/bin/bash
name="Ada"
echo 'Hello, $name'
echo "Hello, $name"
Login to try C/C++/Java/PHP code in the editor
Variables Quote करना क्यों मायने रखता है
एक variable को unquoted छोड़ना Bash को इस पर word splitting (value को spaces/tabs/newlines पर तोड़ना) और pathname expansion apply करने देता है इससे पहले कि command इसे देखे।
Variable को double quotes से quote करना इसकी value को एक single argument के रूप में intact रखता है, जो almost हमेशा वही है जो आप चाहते हैं।
$sentence multiple spaces को single spaces में collapse करता है क्योंकि word splitting extra whitespace discard करता है; quoted "$sentence" इसे exactly preserve करता है।उदाहरण: Why Quoting Variables Matters
#!/bin/bash
sentence="multiple words here"
echo $sentence
echo "---"
echo "$sentence"
Login to try C/C++/Java/PHP code in the editor
एक 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
#!/bin/bash
message='It'\''s a sunny day'
echo "$message"
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
user="ada"
host=$(hostname)
echo "User $user is connected to $host at $(date +%H:%M:%S)"
Login to try C/C++/Java/PHP code in the editor
- यह सोचना कि single और double quotes interchangeable हैं; single quotes सभी expansion (
$variableसहित) suppress करते हैं, जबकि double quotes अब भी variable और command substitution allow करते हैं। - एक variable को unquoted छोड़ना जब इसमें spaces हो सकते हैं, जैसे
echo $nameकी बजायecho "$name", जो word splitting cause करता है और value में spaces या glob characters होने पर command को तोड़ सकता है। - एक 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 के लिए शायद ही कभी वह है जो आप चाहते हैं।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: