← Back to Bash Course | Chapter 8: Input/Output & Redirection | Lesson 2 of 13

read कमांड और Here-Strings

read command एक script को text की एक line पकड़ने और इसे एक variable में store करने देता है, और एक here-string इसे बिना keyboard पर कुछ type किए text feed करने का एक quick तरीका है।
Syntax
bash
read -r variable <<< "text"
read -r var1 var2 <<< "text with words"
read -r -a array_name <<< "text with words"

Reading a Value Without Real stdin

read builtin अपने input से एक line पढ़ता है और इसे एक या कई variables में store करता है। एक here-string (<<<) आपको एक literal string ऐसे देता है जैसे वह type की गई हो, जो बिना किसी interactive terminal attached वाले scripts के लिए perfect है।

Note: हमेशा -r को prefer करें जब तक आप specifically backslash escapes interpreted न चाहें।

उदाहरण: Reading a Value Without Real stdin

bash
#!/bin/bash
read -r name <<< "Ada Lovelace"
echo "Hello, $name"

Reading Multiple Variables at Once

जब read को कई variable names दी जाएं, यह input को IFS पर split करता है (default रूप से whitespace) और हर variable में एक field assign करता है। कोई भी बचे हुए words सब आखिरी variable में stuff हो जाते हैं।

उदाहरण: Reading Multiple Variables at Once

bash
#!/bin/bash
read -r first last <<< "Grace Hopper"
echo "First: $first"
echo "Last: $last"

read -r a b c <<< "one two three four"
echo "c holds the rest: $c"

Reading Into an Array

-a flag read को बताता है कि input को IFS पर split करे और हर field को एक indexed array में एक ही बार में store करे, जो कई named variables declare करने से अक्सर cleaner है।

उदाहरण: Reading Into an Array

bash
#!/bin/bash
read -r -a words <<< "bash scripting is fun"
echo "Word count: ${#words[@]}"
echo "Second word: ${words[1]}"

Custom Field Separator with IFS

आप सिर्फ एक read call के लिए temporarily IFS बदल सकते हैं whitespace से अलग किसी चीज़ पर split करने के लिए, जैसे एक colon, जो /etc/passwd entries जैसी lines parse करने के लिए handy है।

Note: Command से ठीक पहले IFS= set करना सिर्फ उस single command को affect करता है, इसलिए यह script के बाकी हिस्से में leak नहीं होता।

उदाहरण: Custom Field Separator with IFS

bash
#!/bin/bash
IFS=':' read -r user pass uid <<< "root:x:0"
echo "User: $user, UID: $uid"
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. यह मान लेना कि read हमेशा किसी human के कुछ type करने का इंतज़ार करता है; यह खुशी से किसी भी input source से पढ़ता है, एक here-string या एक file शामिल।
  2. यह भूल जाना कि read बिना -r के backslashes को escape characters के रूप में interpret करेगा, C:\temp जैसे paths silently mangle करते हुए।
  3. यह न realize करना कि एक here-string command को feed करने से पहले text में automatically एक trailing newline append करता है।
चैप्टर सारांश
  • read var <<< "text" text को read को इस तरह feed करता है जैसे वह typed हो, असली stdin की ज़रूरत के बिना।
  • read -r backslash escaping disable करता है और आपका default होना चाहिए।
  • read a b c <<< "x y z" whitespace (IFS) पर split करके एक साथ कई variables में डालता है।
  • read -a arr <<< "..." whitespace-separated words को सीधे एक array में पढ़ता है।

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.