|
|
|
Shell provides several ways to write data to files using redirection operators. This example covers common file writing patterns. |
|
|
Basic write with > (overwrites existing file): |
|
|
Append with >> (adds to end of file): |
|
|
Write multiple lines with here-document: |
|
|
Here-document with variable expansion: |
|
|
Write without newline using printf: |
|
|
Redirect stdout and stderr: |
|
|
Discard output: |
|
|
Tee - write to file and stdout simultaneously: |
|
|
Write from pipeline: |
|
|
Atomic write (write to temp, then rename): |
|
|
Write with file descriptor: |
|
|
Append with file descriptor: |
|
|
Create file if not exists, don’t overwrite: |
|
|
Truncate file to empty: |
|
|
Bash
Bash provides noclobber option to prevent overwriting: |
|
|
Write to multiple files at once: |
|
|
Write binary data: |
|
|
Write with specific permissions: |
|
|
Process substitution for writing (bash): diff <(echo “a”) <(echo “b”) |
|
|
Write configuration file: |
|
|
Cleanup temporary files: |
|