惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
博客园_首页
博客园 - 【当耐特】
V
Visual Studio Blog
博客园 - 叶小钗
月光博客
月光博客
美团技术团队
J
Java Code Geeks
小众软件
小众软件
Y
Y Combinator Blog
博客园 - Franky
Martin Fowler
Martin Fowler
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Pause Scripts with 'Press Any Key' in Bash, CMD, PowerShe...
Rost · 2026-04-25 · via DEV Community

Rost

Batch files and shell scripts often need a short wait so a double-clicked window or installer log stays visible. Windows CMD has a dedicated pause command. Unix shells use read.

PowerShell sits in the middle and needs an explicit pattern.

This page collects portable snippets and the usual pitfalls (pipes, SSH without TTY, CI).

For more shell references, see the Bash cheat sheet and PowerShell cheatsheet.

For broader tooling workflows, visit Developer Tools: The Complete Guide to Modern Development Workflows.

When to pause (and when not to)

Use a pause when a human is watching an interactive terminal and you want to avoid an instant exit—for example after a .bat file is double-clicked, or after a local maintenance script prints a summary.

Skip pauses in cron, systemd services, CI pipelines, and most SSH one-liners. There is often no keyboard attached and stdin may not be a terminal, so waiting for input can hang forever. In Bash and POSIX sh, test with [ -t 0 ] (or test -t 0) before prompting.

Windows CMD

The pause command prints a localized line such as “Press any key to continue . . .” and waits for a keypress.

:: save as pause-demo.bat
@echo off
echo Hello from CMD
pause

Enter fullscreen mode Exit fullscreen mode

If output is redirected, behavior can differ; for scripts that must run logged to a file, consider timeout /t N for a timed delay instead of an interactive pause.

choice is useful when you want a timed wait or specific keys (menu scripts). It is a separate topic from pause but often appears in the same batch workflows.

PowerShell

PowerShell has no single pause alias that matches CMD in every host. Two common patterns follow.

Wait for Enter only

Simple and works in many hosts including some IDEs.

# pause-demo.ps1
Read-Host 'Press Enter to continue'

Enter fullscreen mode Exit fullscreen mode

This is not “any key”—only Enter counts.

Wait for any key (Windows console)

In Windows PowerShell running in a normal console host, ReadKey waits for a physical key:

# pause-any-key.ps1
Write-Host 'Press any key to continue...'
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')

Enter fullscreen mode Exit fullscreen mode

If $Host.UI or RawUI is not available (some non-interactive hosts, remoting, or constrained environments), this can fail. Wrap such calls in try / catch or detect [Console]::KeyAvailable / host capabilities when you need robustness.

PowerShell 7 on non-Windows platforms may not offer the same ReadKey experience; prefer Read-Host or shell-native read in those cases.

Bash (Linux and macOS)

Classic “any key” style with a visible prompt and no echo of the key:

#!/usr/bin/env bash
read -r -n 1 -s -p $'Press any key to continue...\n'

Enter fullscreen mode Exit fullscreen mode

  • -n 1 — read one character
  • -s — silent (no echo)
  • -r — raw (backslash not special)
  • -p — prompt string

With a friendly guard for automation:

#!/usr/bin/env bash
if [ -t 0 ]; then
  read -r -n 1 -s -p $'Press any key to continue...\n'
fi

Enter fullscreen mode Exit fullscreen mode

macOS notes

Terminal.app and iTerm2 behave like other Unix terminals for Bash. Apple’s default login shell is often Zsh; for interactive Zsh you may use read -k 1 in scripts explicitly run by Zsh. For maximum portability across Linux and macOS, stay with Bash or document #!/usr/bin/env bash at the top of the script.

POSIX sh (portable “press Enter”)

POSIX read does not require read -n, which is not POSIX. The portable pattern is “press Enter to continue”:

#!/bin/sh
printf 'Press Enter to continue... '
read -r _

Enter fullscreen mode Exit fullscreen mode

That is widely supported on dash, ksh, and ash-based systems. True single-character “any key” without Bash extensions is messier; if you need it on minimal sh, document Bash or use stty-based approaches with care (terminal state, portability).

Cross-platform branching

Installer scripts sometimes branch on OS:

  • Windows CMDpause
  • PowerShellReadKey or Read-Host
  • Unixread with [ -t 0 ] guard

In mixed environments, keep the “interactive only” guard so servers and CI never block.

Related reading

Useful links

  • GNU Bash manual — Bash Builtinsread
  • Microsoft Learn — Read-Host and console APIs for advanced hosts