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

Files लिखना

एक file लिखना एक notebook में लिखने जैसा है: एक arrow page erase करके fresh शुरू करता है, जबकि दो arrows नीचे नई lines add करते हैं।

In this page:

  1. Files लिखना
Syntax
bash
echo "text" > file_name
echo "text" >> file_name
printf "format\n" arguments >> file_name

Files लिखना

एक file में लिखने के लिए redirection उपयोग होता है: echo "text" > file.txt इसे overwrite करता है, और >> append करता है। printf को भी ज़्यादा formatted output के लिए उसी तरह उपयोग किया जा सकता है।

Bash में कोई dedicated "write" command नहीं है — लिखना हमेशा output redirection के through ही होता है।

Note: writes के एक batch की शुरुआत में एक बार > उपयोग करें, फिर subsequent lines के लिए >> पर switch करें, ताकि हर बार file को re-truncate करने से बचा जा सके।

उदाहरण: Writing Files

bash
#!/bin/bash
echo "First line" > notes.txt
echo "Second line" >> notes.txt
printf "Third line with %s\n" "formatting" >> notes.txt

cat notes.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. >> मतलब होने पर > उपयोग करना, जो file erase कर देता है।
  2. एक directory में लिखना जो मौजूद नहीं है।
  3. एक command में same file को पढ़ना और लिखना।

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.