How to use this topic

This topic builds on Linux & Command Line and on the PowerShell basics in Operating Systems: the commands themselves are explained there, and these pages are about turning them into scripts. The first six pages cover the basics: what scripting is, running scripts, variables and quoting, input and output, conditions and loops. The next seven are the tools of real scripts: functions, collections, the pipeline, files, text, errors, and structured data. The last five are about scripts in production: scheduling and logging, remote execution, robustness, testing and linting, and working across platforms.

Examples are run (Bash 5.2, PowerShell 7.6), each in an empty folder, and show their exact output, including the error messages of scripts that fail on purpose. Examples that need Windows, a network or administrator rights cannot run here; their output is labeled illustrative.

Pages marked Linux+ cover the shell-scripting material of the CompTIA Linux+ exam (XK0-006), which tests the Bash side; pages also marked Linux Essentials cover the entry-level Linux Professional Institute (LPI) Linux Essentials exam. Microsoft has no PowerShell exam, so the PowerShell half of each page has no tag of its own.

Learning path

  1. 01What Is Scripting?What a script is and when writing one pays off, the difference between Bash and PowerShell, shells and interpreters, and how this topic compares them side by side.BeginnerLinux+Linux Essentials
  2. 02Running Scripts: Shebang, Permissions and Execution PolicyMaking a script run: the shebang line, execute permission and the PATH in Bash, and .ps1 files, execution policy and script signing in PowerShell.BeginnerLinux+Linux Essentials
  3. 03Variables, Quoting and ExpansionStoring values in variables, and the rules that make or break a script: quoting, word splitting and expansion in Bash, and strings, interpolation and here-strings in PowerShell.BeginnerLinux+Linux Essentials
  4. 04Input and OutputWriting output with echo and printf or Write-Output, reading input with read or Read-Host, and why Write-Host and the output stream behave differently.BeginnerLinux+Linux Essentials
  5. 05Conditions, Tests and Exit CodesMaking decisions in scripts: if, test and [[ ]] in Bash, comparison operators in PowerShell, exit codes and what success means to a shell.BeginnerLinux+Linux Essentials
  6. 06LoopsRepeating work in scripts: for and while loops, reading a file line by line safely, foreach and ForEach-Object, and breaking out early.BeginnerLinux+Linux Essentials
  7. 07Functions and ParametersOrganizing scripts into functions, handling arguments with getopts in Bash, and param blocks, validation and advanced functions in PowerShell.IntermediateLinux+Linux Essentials
  8. 08Arrays, Hash Tables and ObjectsStoring collections in scripts: indexed and associative arrays in Bash, and arrays, hash tables and PSCustomObject in PowerShell.Intermediate
  9. 09The Pipeline: Text versus ObjectsThe key difference between the two shells: Bash pipes text between programs, PowerShell pipes objects between cmdlets, and what that means for filtering, sorting and selecting data.IntermediateLinux+Linux Essentials
  10. 10Files, Paths and GlobbingWorking with files in scripts: globbing, safe handling of names with spaces, find, and Get-ChildItem, Test-Path and Join-Path in PowerShell.IntermediateLinux+
  11. 11Text Processing in ScriptsSearching and changing text in scripts with grep, sed, awk and cut, and with Select-String, -match, -replace and -split in PowerShell.IntermediateLinux+
  12. 12Error HandlingMaking scripts fail safely: exit codes, set -euo pipefail and trap in Bash, and terminating errors, try and catch and ErrorActionPreference in PowerShell.IntermediateLinux+
  13. 13JSON, CSV and APIsHandling structured data in scripts: jq and curl in Bash, ConvertFrom-Json, Import-Csv and Invoke-RestMethod in PowerShell, and calling web APIs safely.Intermediate
  14. 14Scheduling and LoggingRunning scripts on a schedule with cron, systemd timers and Windows Task Scheduler, and writing useful logs from scripts.IntermediateLinux+
  15. 15Remote ExecutionRunning commands and scripts on other machines: ssh in scripts, keys and non-interactive use, and PowerShell Remoting with Invoke-Command.Intermediate
  16. 16Writing Robust and Safe ScriptsHabits that make scripts safe to run again and again: idempotency, dry runs, input validation, temporary files, locking, and keeping secrets out of scripts.Intermediate
  17. 17Testing, Linting and StyleChecking scripts before they run in production: ShellCheck and bats for Bash, PSScriptAnalyzer and Pester for PowerShell, and a consistent style.Intermediate
  18. 18Cross-Platform ScriptingScripts that run on more than one system: PowerShell 7 on Linux and macOS, Bash on Windows with WSL and Git Bash, portable POSIX sh, and choosing the right tool.Intermediate

Scripting glossary

Short definitions of the terms used in this topic. The full glossary covers every topic.