← Back to Bash Course | Chapter 9: File Testing & Filesystem | Lesson 8 of 13

Files में Writing और Appending

Scripts text को disk पर permanently एक नई file में लिखकर या एक existing file में और lines add करके save कर सकती हैं।
Syntax
bash
> file_name
for item in list; do
  echo "$item" >> file_name
done

{
  command1
  command2
} > file_name

एक Loop से Multiple Lines लिखना

एक common mistake एक loop के अंदर > डालना है, जो हर बार file को truncate कर देता है। इसके बजाय, loop से पहले एक बार truncate करें और हर subsequent write के लिए >> उपयोग करें।

Note: : > file या सिर्फ > file (बिना किसी command के) एक file को truncate या empty बनाने का एक quick तरीका है।

उदाहरण: Writing Multiple Lines with a Loop

bash
#!/bin/bash
> items.txt
for item in one two three; do
    echo "$item" >> items.txt
done
cat items.txt
rm -f items.txt

Commands को एक Write में Group करना

कई commands को { ...; } में wrap करना और पूरे group को redirect करना उनके combined output को एक single file open के साथ लिखता है, जो हर command को अलग-अलग redirect करने से ज़्यादा efficient है।

उदाहरण: Grouping Commands into One Write

bash
#!/bin/bash
{
    echo "Report generated"
    echo "Item count: 3"
    date +%Y
} > report.txt
cat report.txt
rm -f report.txt

Write Success जांचना

एक redirection fail हो सकता है, उदाहरण के लिए अगर target directory मौजूद न हो या permissions गलत हों। इसके ठीक बाद $? जांचना (या set -e उपयोग करना) एक missing file के साथ silently continue करने के बजाय इसे पकड़ लेता है।

उदाहरण: Checking Write Success

bash
#!/bin/bash
if echo "data" > /tmp/write_test_$$.txt; then
    echo "write succeeded"
    cat /tmp/write_test_$$.txt
    rm -f /tmp/write_test_$$.txt
else
    echo "write failed" >&2
fi

printf से Formatted Text Append करना

printf formatting और newlines पर exact control देता है, जो अक्सर echo से ज़्यादा predictable है, खासकर जब content में variables हों जो dash से शुरू हो सकते हैं।

Note: printf अपने arguments में leading - को कभी एक option के रूप में interpret नहीं करता, कुछ echo implementations के विपरीत।

उदाहरण: Appending Formatted Text with printf

bash
#!/bin/bash
name="-weird-name"
printf '%s: %d\n' "$name" 42 >> log.txt
cat log.txt
rm -f log.txt
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. एक loop में हर line के लिए > repeat करना, जो हर iteration पर file को वापस empty कर देता है इसे build up करने के बजाय।
  2. यह न जांचना कि एक write succeed हुआ या नहीं (जैसे disk full, permission denied), और यह मान लेना कि > कभी fail नहीं होता।
  3. echo से multi-line content लिखना यह realize किए बिना कि printf newlines और formatting पर ज़्यादा precise control देता है।
चैप्टर सारांश
  • एक loop के अंदर > हर iteration पर truncate करता है; इसे loop से पहले एक बार उपयोग करें, फिर इसके अंदर >>।
  • printf '%s\n' "$line" >> file एक single line append करने का एक robust, predictable तरीका है।
  • { ...; } > file से commands को group करना कई commands के combined output को एक single redirection से लिखता है।
  • हमेशा critical writes के बाद $? जांचें (या set -e उपयोग करें) उन scripts में जो failure पर silently continue नहीं करनी चाहिए।

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.