Skip to content
shellmap

add-contentAppend a string to a file — bash >> redirection across all 5 shells

Equivalents in every shell

Bashunix
echo "hello" >> file.txt

Shell-level append redirection. Creates the file if missing, appends if it exists. `tee -a` is the alternative when you also want the output on stdout. Kernel-level appends are atomic for writes ≤ PIPE_BUF (4KB on Linux).

Zshunix
echo "hello" >> file.txt

Same `>>` semantics. Zsh `>>!` overrides `noclobber`. `MULTIOS` allows `echo hi >>file1 >>file2` to append to BOTH at once.

Fishunix
echo "hello" >> file.txt

Same `>>` semantics. Fish has no analog to `tee -a` natively — use the external `tee` binary: `echo hi | tee -a file.txt`.

PowerShellwindows
Add-Content -Path file.txt -Value "hello"

Appends the `.ToString()` of each input to the file. Creates the file if missing. `-NoNewline` omits the trailing newline; `-Encoding utf8` forces UTF-8 (otherwise inherits the same Win PS 5.1 vs pwsh 6+ defaults as `Set-Content`).

cmd.exewindows
echo hello >> file.txt

Same `>>` semantics as Unix. `echo.` (with period, no space) prints an EMPTY line — useful for blank-line separators between appends. cmd `echo` does NOT strip quotes.

Worked examples

Append a timestamped line to a log file

Bash
echo "$(date) - started" >> app.log
PowerShell
Add-Content -Path app.log -Value "$(Get-Date) - started"
cmd.exe
echo %DATE% %TIME% - started >> app.log

Append the contents of one file to another

Bash
cat extra.txt >> main.txt
PowerShell
Get-Content extra.txt | Add-Content main.txt
cmd.exe
type extra.txt >> main.txt

Append without a trailing newline (continue a partial line)

Bash
printf "%s" "tail" >> file.txt
PowerShell
Add-Content -Path file.txt -Value "tail" -NoNewline

Gotchas

  • Like `Set-Content`, `Add-Content` writes `.ToString()` of each input — NOT the formatted console representation. `Get-Process | Add-Content procs.log` produces `System.Diagnostics.Process (firefox)` lines, not the formatted table you'd see in the console. For formatted appends, use `Out-File -Append`; for structured appends, use `Export-Csv -Append` (pwsh 5+).
  • Each `Add-Content` invocation OPENS and CLOSES the target file — for high-frequency appending inside a tight loop, this is dramatically slower than holding a handle. Bulk-friendly alternative: `$sw = [IO.File]::AppendText('app.log'); $sw.WriteLine('hi'); $sw.Close()` keeps the handle open across writes.
  • Append is NOT atomic across multi-line writes — concurrent `Add-Content` calls from two processes can INTERLEAVE lines mid-record. For lock-safe logging, use a `Mutex` plus `[IO.File]::AppendAllLines()`, or route through a dedicated logger (Serilog, NLog, `Microsoft.Extensions.Logging`).
  • On Windows PowerShell 5.1, appending to an existing UTF-8 file with default `-Encoding` will switch the new portion to UTF-16 LE, producing a file with TWO encodings and a BOM in the middle — unreadable by most consumers. ALWAYS pass `-Encoding` matching the file's existing encoding.
  • `-Value` accepts an array — `Add-Content file.txt -Value 'a','b','c'` appends each as its own line. To append a single multi-line block as ONE record, join first: `Add-Content file.txt -Value ("a`nb`nc")`. The distinction matters when downstream tools `Get-Content` and expect per-record alignment.

WSL & PowerShell Core notes

pwsh`Add-Content` is fully cross-platform. The same encoding-default difference as `Set-Content` applies (Windows PS 5.1 UTF-16 LE + BOM vs pwsh 6+ UTF-8 NoBOM). Line endings follow platform conventions (CRLF on Windows, LF elsewhere). For concurrent-writer scenarios, prefer the .NET `[IO.File]::AppendAllLines` (which respects FileShare semantics) over the cmdlet for predictable cross-platform behaviour.
WSLInside WSL bash, `>>` writes LF UTF-8 bytes. Mixing WSL `>>` and Windows-side pwsh `Add-Content` against the SAME log file is a known gotcha: each side appends with its OWN line-ending convention, producing a mixed-EOL file. For shared logs across the boundary, pick one side as the writer and have the other only consume.

Related commands