Pause Scripts with 'Press Any Key' in Bash, CMD, PowerShell, and macOS

Published: (April 25, 2026 at 04:09 AM EDT)
4 min read
Source: Dev.to

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, pause may behave differently.
  • For logged runs, consider timeout /t N for 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.UI or RawUI is unavailable (e.g., non‑interactive hosts, remoting, constrained environments), the call can fail. Wrap it in try / catch or detect [Console]::KeyAvailable / host capabilities for robustness.
  • On PowerShell 7+ running on non‑Windows platforms, ReadKey may not behave the same; prefer Read-Host or the native read command 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 1 read one character
  • -s  silent (no echo)
  • -r  raw (backslash not special)
  • -p  prompt 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

EnvironmentRecommended pause
Windows CMDpause (or timeout /t N for timed delay)
PowerShellRead-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 shprintf …; read -r _ (press Enter)

Always keep an “interactive‑only” guard ([ -t 0 ] or equivalent) to prevent scripts from blocking in automated environments.

0 views
Back to Blog

Related posts

Read more »