When automating server configurations or writing Bash scripts, interactive commands are your worst enemy. If a package manager suddenly halts the script to ask “Are you sure you want to continue? [y/N]”, the entire automation process freezes until a human manually presses the ‘y’ key. While many modern tools offer a -y flag to bypass this, older utilities and custom installation scripts do not. The simplest way to defeat these interactive roadblocks is by using the classic Linux yes command.
What Does the yes Command Do?
The yes command is one of the oldest and simplest utilities in the Unix ecosystem. By itself, if you type yes into the terminal and press Enter, it will continuously output the letter ‘y’ followed by a newline character at an incredibly high speed, stopping only when you press Ctrl + C. While this seems useless on the surface, its true power is unleashed when you pipe this endless stream of affirmations into another command that is waiting for user input.
How to Pipe yes into a Command
The standard syntax involves running the yes command, followed by the pipe operator (|), followed by the interactive command you want to automate.
For example, if you are running an outdated system cleanup script named clean_logs.sh that repeatedly asks “Delete this file? [y/n]” for every single log file, you can automate the approval process completely.
yes | ./clean_logs.sh
As the script runs, every time it pauses and waits for user input, the yes command instantly feeds it a ‘y’ and an Enter keystroke. The script will blaze through the prompts without any human intervention.
Providing Custom Responses
Despite its name, the yes command can output any string of text you desire. This is crucial for scripts that require specific answers, or for scripts where the safest automated answer is actually “no”.
To make the command output “n” instead of “y”, you simply provide the string as an argument:
yes n | ./interactive_script.sh
If an installation script requires you to accept an End User License Agreement by explicitly typing “accept”, you can structure the command like this:
yes accept | ./install_software.sh
Automating the fsck Utility
One of the most common real-world applications for the yes command is repairing corrupted file systems using fsck. When a drive has hundreds of corrupted sectors, fsck will stubbornly ask for permission to fix every single error individually. To force the utility to fix everything automatically, run:
yes | sudo fsck /dev/sda1
Note: While fsck does have a -y flag, using the yes command is an excellent fallback if you forget the flag or are using a restricted version of the utility on an embedded Linux device.
A Warning on CPU Usage
The yes command is highly optimised and will generate gigabytes of text per second if left unchecked. When piped into a script, it correctly pauses and waits for the script to request input. However, if you pipe it into a script that eventually crashes or enters a loop where it stops reading the input but stays active, the yes command can spike your CPU usage to 100% on that specific core. Always test your automated scripts in a safe environment before deploying them to production servers.