PowerShell vs Bash
PowerShell · Reference cheat sheet
PowerShell vs Bash
PowerShell · Reference cheat sheet
📋 Overview
Bash pipelines text; PowerShell pipelines objects. Bash is ubiquitous on Unix; PowerShell is cross-platform (pwsh) with deep .NET integration. Choose based on environment and data shape—many teams use both.
🔧 Core concepts
| Topic | Bash | PowerShell |
|---|---|---|
| Pipeline | Text streams | Objects |
| Variables | $x, strings | $x, typed objects |
| Compare | [[ ]], = | -eq, -lt, … |
| Errors | Exit codes | Error records + exit codes |
| Lists | Arrays / IFS | Object collections |
| JSON | jq | ConvertFrom-Json |
| Common filter | grep/awk | Where-Object / Select-String |
| Script ext | .sh | .ps1 |
Interop: call bash from pwsh and vice versa when tools are installed.
💡 Examples
Same task:
# bash
ls -1 *.log | wc -l
cat file.json | jq '.name'# PowerShell
@(Get-ChildItem *.log).Count
(Get-Content -Raw file.json | ConvertFrom-Json).nameCall the other shell:
bash -lc 'grep -R TODO .'pwsh -NoProfile -Command 'Get-ChildItem | Select-Object Name'Environment vars:
export PORT=8080$env:PORT = '8080'⚠️ Pitfalls
- Assuming
curlmeans the same thing (alias vs binary) across shells. - Quoting rules differ—don’t paste bash snippets into PowerShell unchanged.
- Exit codes: bash
set -evs PowerShell$ErrorActionPreference. - Line endings and shebangs matter when sharing scripts across OSes.
- Performance: spawning many external processes from either shell is costly—batch work.
- Object formatting in PowerShell can look like “broken text” when piped to bash tools—convert explicitly.