String को Words में तोड़ना
In this page:
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
#!/bin/bash
sentence="the quick brown fox"
for word in $sentence; do
echo "Word: $word"
done
Login to try C/C++/Java/PHP code in the editor
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 है।
IFS restore करें, जैसा दिखाया गया है, ताकि script के बाद वाले parts custom delimiter से affect न हों।उदाहरण: Splitting on a Custom Delimiter
#!/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]}"
Login to try C/C++/Java/PHP code in the editor
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
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 करने से बचाता है।
IFS='...' command set करना change को सिर्फ उस एक command तक scope करता है, shell के अपने IFS को बाद में untouched छोड़ते हुए।उदाहरण: Splitting Into an Array with a Custom IFS
#!/bin/bash
line="red:green:blue"
IFS=':' read -a colors <<< "$line"
echo "Colors: ${colors[@]}"
echo "Count: ${#colors[@]}"
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि word splitting quoted variables पर भी होता है; यह सिर्फ unquoted expansions पर apply होता है,
"$var"कभी word-split नहीं होता चाहेIFSजो भी हो। IFSको temporarily बदलने के बाद इसे original value में restore करना भूल जाना; एक customIFSको वैसे ही छोड़ देना script में बाद के हर दूसरे unquoted expansion को silently तोड़ सकता है।- यह मान लेना कि
read -aया एकforloop आपके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
IFSspace, tab, और newline है; इसे बदलना आपको commas जैसे दूसरे characters पर split करने देता है। - Word splitting सिर्फ unquoted expansions पर होती है --
"$var"के साथ एक variable quote करना इसे पूरी तरह disable कर देता है। - Temporarily
IFScustomize करने के बाद हमेशा originalIFSsave और restore करें (या change को एक subshell/local context तक scope करें), ताकि script के बाकी हिस्से पर असर न पड़े।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: