Skip to content
shellmap

sortSort lines of text across all 5 shells

Equivalents in every shell

Bashunix
sort file
Zshunix
sort file
Fishunix
sort file
PowerShellwindows
Get-Content file | Sort-Object

Aliased as `sort`.

cmd.exewindows
sort file

Native cmd command, but limited (no numeric sort flag).

Worked examples

Sort numerically

Bash
sort -n file
PowerShell
Get-Content file | Sort-Object { [int]$_ }

Reverse sort

Bash
sort -r file
PowerShell
Get-Content file | Sort-Object -Descending
cmd.exe
sort /r file

Sort unique lines

Bash
sort -u file
PowerShell
Get-Content file | Sort-Object -Unique

Gotchas

  • GNU `sort -n` sorts numerically; PowerShell `Sort-Object` sorts as strings unless you cast (`{ [int]$_ }`).
  • cmd `sort` is alphabetical only and case-insensitive by default — pass `/c` to make it case-sensitive.
  • Large file sorts: GNU sort uses temporary files on disk; `Sort-Object` is fully in-memory and can OOM on huge inputs.

WSL & PowerShell Core notes

pwsh`Sort-Object` is fully in-memory on every pwsh platform — sorting multi-GB log lines blows the .NET heap, while GNU `sort` happily spills to `$TMPDIR`. For very large inputs on Linux/macOS pwsh hosts, shell out to the system binary (`Get-Content huge.log | sort`) rather than relying on the cmdlet. Locale also matters: GNU `sort` honours `LC_COLLATE` (so `a A b B` interleaves under `en_US.UTF-8`) while `Sort-Object` is ordinal by default — pass `-Culture invariant` for predictable cross-platform results.
WSLSorting a `/mnt/c/...` file from WSL works but the initial read is bottlenecked by DrvFs / 9P bandwidth (~150–250 MB/s typical, vs 1 GB/s+ for native ext4 inside `~/`). GNU `sort`'s external-memory spill files land in `$TMPDIR` which defaults to `/tmp` on the WSL ext4 — that side stays fast. For one-off cleanup of a huge Windows-side CSV, copy to `~/` first (`cp /mnt/c/data/huge.csv ~/`), sort there, then move the result back; you pay the DrvFs cost once instead of once per external-sort pass.

Common tasks using sort

Related commands