Writing Files
In this page:
Writing Files
Writing to a file uses redirection: echo "text" > file.txt overwrites it, and >> appends. printf can be used the same way for more formatted output. There's no dedicated "write" command in Bash — writing is always done through output redirection.
Note: Use > once at the start of a batch of writes, then switch to >> for subsequent lines, to avoid re-truncating the file each time.
Example: Writing Files
#!/bin/bash
echo "First line" > notes.txt
echo "Second line" >> notes.txt
printf "Third line with %s\n" "formatting" >> notes.txt
cat notes.txt
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: