Code Reference

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

TopicBashPowerShell
PipelineText streamsObjects
Variables$x, strings$x, typed objects
Compare[[ ]], =-eq, -lt, …
ErrorsExit codesError records + exit codes
ListsArrays / IFSObject collections
JSONjqConvertFrom-Json
Common filtergrep/awkWhere-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).name

Call 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 curl means the same thing (alias vs binary) across shells.
  • Quoting rules differ—don’t paste bash snippets into PowerShell unchanged.
  • Exit codes: bash set -e vs 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.

On this page