← Back to Bash Course | Chapter 6: Arrays | Lesson 10 of 10

Command Output को Array में पढ़ना

Bash एक command के output को directly एक array में capture कर सकता है, एक line में एक element, बिना आपको manually loop करके और हर line खुद append करना पड़े।
Syntax
bash
mapfile -t array_name < file_or_input
readarray -t array_name <<< "$data"

mapfile / readarray Basics

mapfile -t arr standard input को line by line पढ़ता है और हर line को arr के एक element के रूप में store करता है, -t हर line की trailing newline strip करते हुए। बिना manual looping के multi-line text को एक array में बदलने का यह सबसे clean तरीका है।

उदाहरण: mapfile / readarray Basics

bash
#!/bin/bash
data=$'apple\nbanana\ncherry'
mapfile -t fruits <<< "$data"
echo "Element count: ${#fruits[@]}"
echo "Second fruit: ${fruits[1]}"

readarray mapfile जैसा ही है

readarray बिल्कुल वही builtin है जो mapfile है, बस एक alternate नाम के under -- दोनों हर script में पूरी तरह interchangeable हैं। कुछ scripts stylistic reasons के लिए एक नाम उपयोग करते हैं, लेकिन कोई functional difference नहीं है।

उदाहरण: readarray Is the Same as mapfile

bash
#!/bin/bash
printf "line one\nline two\nline three\n" > lines.txt
readarray -t lines < lines.txt
for line in "${lines[@]}"; do
    echo "Read: $line"
done

-t क्यों मायने रखता है

-t flag के बिना, mapfile/readarray हर captured element के end में trailing newline character attached रखता है, जो बाद में उन values को compare या display करते समय subtle bugs cause कर सकता है।

-t उपयोग करना इसे strip करता है, clean line content देते हुए।

उदाहरण: Why -t Matters

bash
#!/bin/bash
data=$'first\nsecond'
mapfile -t clean_lines <<< "$data"
mapfile raw_lines <<< "$data"
echo "Clean (with -t): [${clean_lines[0]}]"
echo "Raw (without -t): [${raw_lines[0]}]"

एक Single Line Split करने के लिए read -a

read -a arr input की एक line को IFS (default रूप से whitespace) के आधार पर array elements में split करता है, जो mapfile के काम से अलग है जो multiple lines को multiple elements में बदलता है।

यह एक space-separated line के fields parse करने के लिए handy है।

उदाहरण: read -a for Splitting a Single Line

bash
#!/bin/bash
read -a fields <<< "red green blue"
echo "Number of fields: ${#fields[@]}"
echo "Second field: ${fields[1]}"
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. arr=($(command)) उपयोग करना और यह मान लेना कि हर line एक element बन जाता है; वह form असल में सभी whitespace पर word-split करता है (सिर्फ newlines पर नहीं), इसलिए multiple words वाली एक line multiple array elements में टूट जाती है।
  2. यह भूल जाना कि mapfile/readarray (वे same command हैं, readarray एक alias है) Bash 4.0+ features हैं और बहुत पुराने Bash या sh पर काम नहीं करेंगे।
  3. mapfile/readarray के साथ -t उपयोग न करना, जो हर captured element के end में trailing newline character attached छोड़ देता है।
चैप्टर सारांश
  • mapfile -t arr <<< "$data" (या < file) input को directly line by line एक array में पढ़ता है, एक line में एक element।
  • readarray सिर्फ बिल्कुल वही builtin का एक दूसरा नाम है जो mapfile है -- वे पूरी तरह interchangeable हैं।
  • -t flag हर line की store होने से पहले trailing newline strip करता है -- जो almost always वही है जो आप चाहते हैं।
  • read -a arr <<< "$line" एक single line को whitespace से (IFS उपयोग करते हुए) एक array में split करता है, जो multiple lines पढ़ने से अलग use case है।

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.