Prevent Unrar Hanging on Password Prompt
When automating extraction tasks using scripts, cron jobs, or CI/CD
pipelines, the unrar command often hangs indefinitely if it
encounters a password-protected archive because it pauses execution
waiting for interactive user input via standard input. This article
explains how to prevent unrar from blocking terminal
execution by disabling interactive password prompts, forcing immediate
failure on encrypted files, and properly handling passwords in
non-interactive environments.
The Root Cause
By default, when unrar encounters an encrypted archive
or encrypted file headers without receiving a password parameter, it
initiates an interactive prompt:
Enter password (will not be echoed):
In automated or headless environments where no human is present to
type a password, standard input (stdin) remains open,
causing the command to freeze and hang indefinitely.
Method 1: Use the -p-
Switch
The most effective and direct way to stop unrar from
asking for a password is the -p- flag. This switch
explicitly instructs the utility to disable the password query
completely.
unrar x -p- archive.rar- If the archive is unencrypted,
unrarextracts it normally. - If the archive is encrypted,
unraraborts the operation immediately with a non-zero exit code instead of waiting for input.
Method 2:
Redirect Standard Input from /dev/null
If you are using legacy versions of unrar or want an
extra layer of protection across shell environments, redirect standard
input from /dev/null. This sends an immediate End-Of-File
(EOF) signal whenever unrar tries to read input from the
console.
unrar x archive.rar < /dev/nullWhen unrar attempts to read the password, it immediately
reads EOF, fails the prompt, and terminates execution.
Method 3: Pass a Known Password Non-Interactively
If you expect password-protected files and have the credentials,
supply the password directly in the command line using
-p<password>. Note that there must be no
space between the -p flag and the password
itself.
unrar x -pMySecretPassword archive.rarTo avoid exposing passwords in plain text within system process
tables (e.g., via ps aux), pass the password through an
environment variable or standard input redirection if supported by your
wrapper script.
Method 4: Combine Flags for Fully Automated Extraction
For robust, headless automation, combine non-interactive password suppression with automatic file overwrite handling and error output redirection:
unrar x -inul -o+ -p- archive.rar < /dev/null-inul: Suppresses all error and informational messages.-o+: Overwrites existing files without prompting for confirmation.-p-: Disables the interactive password prompt.< /dev/null: Ensures stdin closes immediately.