← Back to Bash Course | Chapter 7: String Manipulation | Lesson 9 of 10

String जोड़ना

Bash में strings को साथ जोड़ना इन्हें एक-दूसरे के पास लिखने या += से append करने जितना simple है, कोई special concatenation operator चाहिए ही नहीं।
Syntax
bash
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

bash
#!/bin/bash
first="Hello"
second="World"
combined="$first, $second!"
echo "$combined"

Appending with +=

+= operator एक existing string variable के आखिर में नया text append करता है, plain = की तरह इसकी current value replace करने के बजाय। यह incrementally एक string build करने के लिए convenient है, जैसे एक loop के अंदर।

उदाहरण: Appending with +=

bash
#!/bin/bash
message="Processing"
message+=": "
message+="step 1"
message+=", step 2"
echo "$message"

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

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

Concatenating Numbers as Strings

चूंकि हर Bash variable default रूप से text है, numbers जैसी दिखने वाली चीज़ों को concatenate करना सिर्फ उनके characters को जोड़ता है जोड़ने के बजाय, $(( )) के अंदर उपयोग करने के विपरीत जहां वे sum होतीं।

यह जानना कि आप किस context में हैं -- string concatenation vs. arithmetic -- confusion के एक common source से बचाता है।

उदाहरण: Concatenating Numbers as Strings

bash
#!/bin/bash
a="12"
b="34"
concatenated="$a$b"
sum=$((a + b))
echo "Concatenated (string): $concatenated"
echo "Sum (arithmetic): $sum"
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. दूसरी languages जैसे + या . concatenation operator ढूंढना; Bash बिना किसी operator के ही values को एक string के अंदर adjacent रखकर concatenate करता है।
  2. यह भूल जाना कि += एक string variable की existing value में append करता है इसे replace करने के बजाय, जो एक plain = के opposite है।
  3. 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 में उपयोग न हो।

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.