Stuck with a script that’s flying through commands too fast? You’ve got two solid options: pause for a manual stop or sleep for a timed delay.
What’s Happening
Running a script in Windows Command Prompt (cmd.exe) or PowerShell? You might want to pause execution to catch messages or inspect things before it keeps going. In a classic .bat or .cmd file, pause is built right in. Unix-like shells (Linux, macOS Terminal, WSL) don’t have a single “pause” command, so you’ll use sleep or read instead. Try typing pause in Bash, and you’ll just get “command not found”—that’s why the right tool matters for the right system.
Step-by-Step Solution
Pause a Windows Batch Script
pause command, which halts execution until you press a key.- Open Command Prompt (press Win + R, type
cmd, then hit Enter). - Head to your script folder:
cd C:\Scripts - Create or edit
pause_demo.cmdin Notepad:@echo off echo Starting backup... pause echo Backup complete.
- Save the file and run it:
pause_demo.cmd - The script stops at
pauseand waits for any key. Press any key to continue.
Pause a Shell Script on Linux/macOS/WSL
sleep is the go-to for timed pauses in shell scripts.- Open a terminal.
- Create
sleep_demo.shwith this content:#!/bin/bash echo "Waiting 5 seconds..." sleep 5 echo "Done waiting."
- Make it executable:
chmod +x sleep_demo.sh - Run it:
./sleep_demo.sh
Pause a Shell Script Until User Presses Enter
read to pause until Enter is pressed.- Edit the same file:
#!/bin/bash echo "Press Enter to continue..." read -r
- Run it again. The script halts until you press Enter.
If This Didn’t Work
- On Windows: Want to hide the “Press any key to continue…” message? Just add
nulto the pause command:pause >nul. It still waits for a keypress but keeps the prompt clean. - On Linux/macOS: If
sleepisn’t recognized, you might not be using Bash. Tryread -p "Press Enter to continue..."or install coreutils if it’s missing. - Keyboard stuck? If the Pause key on your keyboard isn’t responding, your system might be intercepting it. Try Ctrl+S to pause output or fall back on the step-by-step method above.
Prevention Tips
Want to avoid messy batch script surprises? Start with @echo off so only your messages show up, not every command being executed. Need a 5-second delay without user input? timeout /t 5 works great for automated installs. In Bash scripts, always quote variables ("$var") and test with #!/bin/bash shebang to dodge silent failures. Keep a simple test script handy: echo Hello && pause (Windows) or #!/bin/bash echo Hello; read -r (Linux). Run it whenever you tweak your environment—it’s a lifesaver.
