Pause Scripts with 'Press Any Key' in Bash, CMD, PowerShell, and macOS
Source: Dev.to
Overview
A short pause is often useful when a script is run interactively (e.g., double‑clicked .bat files or maintenance scripts that print a summary). It keeps the window open long enough for a human to read the output.
Do not use pauses in non‑interactive contexts such as cron, systemd services, CI pipelines, or most SSH one‑liners—stdin may not be a terminal, causing the script to hang. In Bash and POSIX sh, test with [ -t 0 ] (or test -t 0) before prompting.
Windows CMD
The built‑in pause command prints a localized message like “Press any key to continue …” and waits for a keypress.
:: save as pause-demo.bat
@echo off
echo Hello from CMD
pause
- If output is redirected,
pausemay behave differently. - For logged runs, consider
timeout /t Nfor a timed delay instead of an interactive pause.
choice can be used for timed waits or specific keys, but it is a separate topic from pause.
PowerShell
PowerShell has no single alias that matches CMD’s pause. Two common patterns are:
Prompt for Enter (works in many hosts)
# pause-demo.ps1
Read-Host 'Press Enter to continue'
Only the Enter key is accepted.
Prompt for any key (requires a console host)
# pause-any-key.ps1
Write-Host 'Press any key to continue...'
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
- If
$Host.UIorRawUIis unavailable (e.g., non‑interactive hosts, remoting, constrained environments), the call can fail. Wrap it intry / catchor detect[Console]::KeyAvailable/ host capabilities for robustness. - On PowerShell 7+ running on non‑Windows platforms,
ReadKeymay not behave the same; preferRead-Hostor the nativereadcommand in those cases.
Bash (Linux/macOS)
A classic “any key” pause with a visible prompt and no echo:
#!/usr/bin/env bash
read -r -n 1 -s -p $'Press any key to continue...\n'
-n 1read one character-ssilent (no echo)-rraw (backslash not special)-pprompt string
Guard for non‑interactive environments
#!/usr/bin/env bash
if [ -t 0 ]; then
read -r -n 1 -s -p $'Press any key to continue...\n'
fi
macOS terminals
Both Terminal.app and iTerm2 behave like other Unix terminals. macOS’s default login shell is often zsh; for scripts explicitly run by zsh, you can use read -k 1. For maximum portability across Linux and macOS, stick with Bash or document #!/usr/bin/env bash at the top of the script.
POSIX sh (portable “press Enter”)
The POSIX read builtin does not require -n, which is non‑standard. A portable “press Enter” pattern works everywhere:
#!/bin/sh
printf 'Press Enter to continue... '
read -r _
This works on dash, ksh, ash, and other POSIX‑compliant shells. Implementing a true single‑character “any key” pause without Bash extensions is more complex (e.g., using stty), and is generally discouraged for minimal sh scripts.
Cross‑Platform Recommendations
| Environment | Recommended pause |
|---|---|
| Windows CMD | pause (or timeout /t N for timed delay) |
| PowerShell | Read-Host for Enter; $Host.UI.RawUI.ReadKey() for any key (with fallback) |
| Bash (Linux/macOS) | read -r -n 1 -s -p ... with [ -t 0 ] guard |
POSIX sh | printf …; read -r _ (press Enter) |
Always keep an “interactive‑only” guard ([ -t 0 ] or equivalent) to prevent scripts from blocking in automated environments.
Useful Links
- GNU Bash Manual – Builtins –
read - Microsoft Learn –
Read-Hostand console APIs for advanced hosts - Bash cheat sheet – general command reference
- PowerShell cheatsheet – cmdlets and everyday usage