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

String को Words में तोड़ना

Bash अपने आप unquoted text को अलग words में तोड़ देता है जहां भी special IFS variable में listed characters मिलें, जो normally सिर्फ space, tab, और newline होते हैं।
Syntax
bash
for word in $string; do
  commands using "$word"
done

IFS=',' read -r -a array_name <<< "$string"

The Default IFS Behavior

Default रूप से, IFS में एक space, एक tab, और एक newline होता है, और Bash इन characters का उपयोग करके unquoted variable expansions को अलग words में split करता है।

यही वजह है कि एक plain for word in $sentence loop naturally हर space-separated word पर iterate करता है।

उदाहरण: The Default IFS Behavior

bash
#!/bin/bash
sentence="the quick brown fox"
for word in $sentence; do
    echo "Word: $word"
done

Splitting on a Custom Delimiter

IFS को temporarily किसी अलग character में set करना, जैसे एक comma, यह बदल देता है कि Bash unquoted expansions और read के लिए किसे word boundary मानता है। यह CSV-style lines जैसा simple delimited data parse करने के लिए एक common technique है।

Warning: हमेशा बाद में original IFS restore करें, जैसा दिखाया गया है, ताकि script के बाद वाले parts custom delimiter से affect न हों।

उदाहरण: Splitting on a Custom Delimiter

bash
#!/bin/bash
csv_line="apple,banana,cherry"
old_ifs="$IFS"
IFS=","
read -a fields <<< "$csv_line"
IFS="$old_ifs"
echo "Field count: ${#fields[@]}"
echo "Second field: ${fields[1]}"

Quoting Prevents Word Splitting

एक variable expansion को double quotes में wrap करना word splitting को पूरी तरह disable कर देता है, पूरी value को एक unit के रूप में रखते हुए चाहे IFS जो भी set हो।

यही कारण है कि variables को quote करना default safe habit है, और unquoted expansion उन situations के लिए reserved है जहां आप specifically splitting चाहते हों।

उदाहरण: Quoting Prevents Word Splitting

bash
#!/bin/bash
data="one two three"
echo "Quoted (one word): \"$data\""
count=0
for part in $data; do
    count=$((count + 1))
done
echo "Unquoted split into $count words"

Splitting Into an Array with a Custom IFS

एक custom IFS को read -a के साथ combine करना एक single delimited line को एक step में सीधे एक array में split करने का एक clean तरीका है। IFS change को सिर्फ उस एक command तक scope करना (इसे read से पहले inline set करके) इसे manually save और restore करने से बचाता है।

Warning: उसी line पर command के साथ IFS='...' command set करना change को सिर्फ उस एक command तक scope करता है, shell के अपने IFS को बाद में untouched छोड़ते हुए।

उदाहरण: Splitting Into an Array with a Custom IFS

bash
#!/bin/bash
line="red:green:blue"
IFS=':' read -a colors <<< "$line"
echo "Colors: ${colors[@]}"
echo "Count: ${#colors[@]}"
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. यह मान लेना कि word splitting quoted variables पर भी होता है; यह सिर्फ unquoted expansions पर apply होता है, "$var" कभी word-split नहीं होता चाहे IFS जो भी हो।
  2. IFS को temporarily बदलने के बाद इसे original value में restore करना भूल जाना; एक custom IFS को वैसे ही छोड़ देना script में बाद के हर दूसरे unquoted expansion को silently तोड़ सकता है।
  3. यह मान लेना कि read -a या एक for loop आपके IFS में set exact delimiter string पर split करता है; यह IFS में मौजूद *किसी भी* single character पर split करता है, पूरी string को एक multi-character delimiter के रूप में नहीं।
चैप्टर सारांश
  • IFS (Internal Field Separator) यह define करता है कि unquoted expansion के दौरान और read द्वारा किन characters को word boundary माना जाए।
  • Default IFS space, tab, और newline है; इसे बदलना आपको commas जैसे दूसरे characters पर split करने देता है।
  • Word splitting सिर्फ unquoted expansions पर होती है -- "$var" के साथ एक variable quote करना इसे पूरी तरह disable कर देता है।
  • Temporarily IFS customize करने के बाद हमेशा original IFS save और restore करें (या change को एक subshell/local context तक scope करें), ताकि script के बाकी हिस्से पर असर न पड़े।

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.