read कमांड और Here-Strings
In this page:
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 है।
-r को prefer करें जब तक आप specifically backslash escapes interpreted न चाहें।उदाहरण: Reading a Value Without Real stdin
#!/bin/bash
read -r name <<< "Ada Lovelace"
echo "Hello, $name"
Login to try C/C++/Java/PHP code in the editor
Reading Multiple Variables at Once
जब read को कई variable names दी जाएं, यह input को IFS पर split करता है (default रूप से whitespace) और हर variable में एक field assign करता है। कोई भी बचे हुए words सब आखिरी variable में stuff हो जाते हैं।
उदाहरण: Reading Multiple Variables at Once
#!/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"
Login to try C/C++/Java/PHP code in the editor
Reading Into an Array
-a flag read को बताता है कि input को IFS पर split करे और हर field को एक indexed array में एक ही बार में store करे, जो कई named variables declare करने से अक्सर cleaner है।
उदाहरण: Reading Into an Array
#!/bin/bash
read -r -a words <<< "bash scripting is fun"
echo "Word count: ${#words[@]}"
echo "Second word: ${words[1]}"
Login to try C/C++/Java/PHP code in the editor
Custom Field Separator with IFS
आप सिर्फ एक read call के लिए temporarily IFS बदल सकते हैं whitespace से अलग किसी चीज़ पर split करने के लिए, जैसे एक colon, जो /etc/passwd entries जैसी lines parse करने के लिए handy है।
IFS= set करना सिर्फ उस single command को affect करता है, इसलिए यह script के बाकी हिस्से में leak नहीं होता।उदाहरण: Custom Field Separator with IFS
#!/bin/bash
IFS=':' read -r user pass uid <<< "root:x:0"
echo "User: $user, UID: $uid"
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
readहमेशा किसी human के कुछ type करने का इंतज़ार करता है; यह खुशी से किसी भी input source से पढ़ता है, एक here-string या एक file शामिल। - यह भूल जाना कि
readबिना-rके backslashes को escape characters के रूप में interpret करेगा,C:\tempजैसे paths silently mangle करते हुए। - यह न realize करना कि एक here-string command को feed करने से पहले text में automatically एक trailing newline append करता है।
read var <<< "text"textकोreadको इस तरह feed करता है जैसे वह typed हो, असली stdin की ज़रूरत के बिना।read -rbackslash escaping disable करता है और आपका default होना चाहिए।read a b c <<< "x y z"whitespace (IFS) पर split करके एक साथ कई variables में डालता है।read -a arr <<< "..."whitespace-separated words को सीधे एक array में पढ़ता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- Function Basics
- The read Command and Here-Strings
- Function Arguments
- stdin, stdout, stderr and File Descriptors
- Return Values
- Redirection Operators
- Local Variables
- Pipes and Chaining Commands
- Recursive Functions
- Here-Documents (<<EOF)
- Function Libraries
- The tee Command
- Discarding Output with /dev/null