Skip to content
shellmap

printfPrint formatted text using C-style format specifiers like %s and %d across all 5 shells

Equivalents in every shell

Bashunix
printf "%s\n" "hello"

Bash builtin (with the same name as `/usr/bin/printf`). Supports `%s`, `%d`, `%x`, `%f`, `%b` (interpret backslash escapes), and printf-style argument re-use.

Zshunix
printf "%s\n" "hello"

Zsh builtin; identical to bash.

Fishunix
printf "%s\n" hello

Fish calls the external `printf` binary (`/usr/bin/printf` on Linux/macOS, `printf.exe` from Git Bash on Windows). Same format-string semantics; no builtin.

PowerShellwindows
"{0}`n" -f "hello"

No `printf` cmdlet. Use the `-f` format operator (`{0}`, `{1}` placeholders) or `[string]::Format()`. `Write-Host -NoNewline` combined with `-f` is the closest in-place printf substitute.

cmd.exewindows
echo hello

No printf. `echo` always appends a newline; for formatting use PowerShell, Git Bash, or WSL.

Worked examples

Print a labeled value with no trailing newline

Bash
printf "name: %s" "ada"
PowerShell
Write-Host ("name: {0}" -f "ada") -NoNewline

Zero-pad a number to four digits

Bash
printf "%04d\n" 42
Zsh
printf "%04d\n" 42
PowerShell
"{0:0000}" -f 42

Print each array element on its own line

Bash
printf "%s\n" "${arr[@]}"
Fish
printf "%s\n" $arr
PowerShell
$arr | ForEach-Object { $_ }

Gotchas

  • Bash builtin `printf` and `/usr/bin/printf` differ subtly — the builtin understands `%q` (shell-quote), the external usually does not. `command printf` forces the external binary.
  • `printf "$user_input"` is a *format-string injection vulnerability* — `%s` in user input pulls from the argument list. Always pass user data as an argument: `printf "%s" "$user_input"`.
  • PowerShell's `-f` operator uses .NET `string.Format` syntax (`{0:N2}`), NOT C printf (`%.2f`). They cover similar ground but flags, padding, and locale handling all differ — port carefully.
  • Fish's `printf` is external, so it shells out for every call — measurably slower than bash's builtin in tight loops. For per-element formatting in fish, prefer `string` builtins where possible.
  • Cmd has no printf. The usual hack `echo X` is line-buffered and trims trailing whitespace; for any non-trivial formatting drop to PowerShell with `cmd /c powershell -c "..."`, or call a Git Bash / WSL printf.

WSL & PowerShell Core notes

pwshPowerShell Core on Linux/macOS still has no `printf` cmdlet — the system `printf` binary is reachable as `/usr/bin/printf`, but it bypasses pwsh formatting. The cross-platform answer is always `-f` or `[string]::Format()`.
WSLWSL `printf` is the bash/POSIX builtin and behaves identically to native Linux. There is no Windows-side `printf.exe` — only Git Bash or WSL itself.

Common tasks using printf

Related commands