String जोड़ना
In this page:
combined="$string1$string2"
variable+="more text"
Basic Adjacent Concatenation
Bash strings को सिर्फ एक-दूसरे के पास बिना किसी operator के रखकर concatenate करता है, चाहे वे दो variables हों, एक variable और literal text हो, या कई pieces combined हों।
पूरी चीज़ को double quotes में wrap करना इसे एक clean result के रूप में रखता है।
उदाहरण: Basic Adjacent Concatenation
#!/bin/bash
first="Hello"
second="World"
combined="$first, $second!"
echo "$combined"
Login to try C/C++/Java/PHP code in the editor
Appending with +=
+= operator एक existing string variable के आखिर में नया text append करता है, plain = की तरह इसकी current value replace करने के बजाय। यह incrementally एक string build करने के लिए convenient है, जैसे एक loop के अंदर।
उदाहरण: Appending with +=
#!/bin/bash
message="Processing"
message+=": "
message+="step 1"
message+=", step 2"
echo "$message"
Login to try C/C++/Java/PHP code in the editor
Building a String Inside a Loop
एक loop के अंदर बार-बार += उपयोग करना कई pieces से एक result string accumulate करने का एक common तरीका है, जैसे array elements को एक separator के साथ जोड़ना। यह सिर्फ एक list of values join करने के लिए एक external tool के overhead और complexity से बचाता है।
उदाहरण: Building a String Inside a Loop
#!/bin/bash
items=(apple banana cherry)
joined=""
for item in "${items[@]}"; do
if [ -z "$joined" ]; then
joined="$item"
else
joined+=", $item"
fi
done
echo "Joined list: $joined"
Login to try C/C++/Java/PHP code in the editor
Concatenating Numbers as Strings
चूंकि हर Bash variable default रूप से text है, numbers जैसी दिखने वाली चीज़ों को concatenate करना सिर्फ उनके characters को जोड़ता है जोड़ने के बजाय, $(( )) के अंदर उपयोग करने के विपरीत जहां वे sum होतीं।
यह जानना कि आप किस context में हैं -- string concatenation vs. arithmetic -- confusion के एक common source से बचाता है।
उदाहरण: Concatenating Numbers as Strings
#!/bin/bash
a="12"
b="34"
concatenated="$a$b"
sum=$((a + b))
echo "Concatenated (string): $concatenated"
echo "Sum (arithmetic): $sum"
Login to try C/C++/Java/PHP code in the editor
- दूसरी languages जैसे
+या.concatenation operator ढूंढना; Bash बिना किसी operator के ही values को एक string के अंदर adjacent रखकर concatenate करता है। - यह भूल जाना कि
+=एक string variable की existing value में append करता है इसे replace करने के बजाय, जो एक plain=के opposite है। - Unquoted variables concatenate करना जिनमें spaces या globs हो सकते हैं, जो एक clean literal join के बजाय word splitting या pathname expansion trigger कर सकता है।
- Concatenation values को एक-दूसरे के पास रखकर automatically होता है:
combined="$a$b"याcombined="$a $b"। var+="more text"एक existing string variable की value के आखिर में append करता है।- Double quotes के अंदर concatenate करना सबसे safe approach है, क्योंकि यह join हो रहे pieces पर unwanted word splitting या globbing रोकता है।
- Numbers और strings किसी भी दूसरी value जैसे ही concatenate होते हैं, क्योंकि हर चीज़ text है जब तक एक explicit arithmetic context में उपयोग न हो।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: