Want to learn how to use the Linux yes command? In this tutorial, Iβll show you everything you need to know about yes, a simple but powerful Linux utility that can automatically confirm or decline prompts, repeat text endlessly, and even help with automation.
Weβll start by checking if the yes command is installed, then walk through its basic usage, how to use it to auto-confirm prompts, and even how to make it automatically decline prompts. Along the way, Iβll also explain why some commands donβt work with yes and share several real-world examples of how this command can save you time.
Whether youβre a Linux beginner or just looking for new ways to streamline your workflow, this video will give you practical examples of the yes command in action.
Basic Usage
As always, we’ll begin by taking a look at how to install the command that’s featured in this article. However, the yes command is more than likely something you’ll already have access to:
command -v yes
If for some reason you don’t see any output that means the yes command is not available on your system. And this is rare, because most distributions include this. But if yours doesn’t, all you should have to do is install the coreutils package – this package includes a number of basic utilities, the yes command included. Most distributions should have this package available, so all you should have to do is install it. Again, it’s going to be rare for the yes command to not be available already, but I’m sure someone within my audience is using a distribution that’s built differently.
For example:
sudo apt install coreutils
or
sudo dnf install coreutils
After installing it, you should see output when we check for the command:
command -v yes
Now that we’ve covered how to install the yes command let’s take a look at basic usage of the yes command. And the first examples I’m going to give you aren’t going to be all that exciting, but we’ll get to more useful examples later. For now, let’s make sure we understand the yes command in its most basic form. All you have to do is type yes and press enter.
As you can see, it’s simply printing ‘y’ over and over. Not very useful, I know – but when it comes to using the yes command with no options, that’s what it does.
yes
Another basic use-case of the yes command is to print a string repeatedly:
yes "Hello World"
The way this works, is whatever string you enter after the yes command is printed over and over, instead of it printing y as it does with no options.
Earlier, I mentioned that the yes command can be used to answer yes to a prompt, which is useful for automation. Here’s an example of that.
sudo apt remove vim-nox && sudo apt autoremove
Let’s say for example we want to install a package on our system. I’m going to use the vim-nox package as an example, it’s a package that’s available for Debian and Ubuntu. But it doesn’t matter what package you install, I just randomly chose this as an example. I’m not going to confirm this command though, but I wanted to remind you of what the prompt looks like when you go to install something.
sudo apt install vim-nox
In this case, it’s going to install a number of dependencies, and it’s asking me whether or not I want to continue. I’m going to answer no for now.
Anyway, with the yes command, we can automate answering yes to the confirmation prompt. Before I show you that though, I want to point out that if a package doesn’t have any dependencies to install, then depending on your package manager – it will confirm it automatically. At least in Debian, when you go to install something and needs to pull in dependencies, it’s going to ask you first. But if we combine the apt install command with yes, it’ll automatically confirm it.
yes | sudo apt install vim-nox
As you can see, the package and its dependencies were automatically confirmed and the process carried out automatically. Of course, if all you wanted to do was automatically answer yes for confirming package installation, then package managers usually have a built-in option to skip confirmation – so you don’t actually need the yes command for this. In the case of apt, you can use -y to automatically confirm.
sudo apt install -y vim-nox
Regardless, even though the apt package manager has a built-in option to auto-confirm, I wanted to show you an example of what happens when you use the yes command with another command. Eventually, you’ll run into a command that might not have a built-in confirm option, in which case the yes command can be used.
Next, let’s see another example. I’m going to use the vim-nox package again for this one. To reset our example, I’m going to remove vim-nox from my system:
sudo apt remove --auto-remove vim-nox
With this command, I’m uninstalling the vim-nox package to reset our example. I used the –auto-remove option to also have it remove dependencies as well.
Anyway, the next example I’m going to give you is to automatically enter no for a prompt.
yesn | sudo apt install vim-nox
In this case, we’re using “n” as an argument to the yes command. To understand this example better, let’s see what happens when we provide n as an argument to the yes command:
yes n
As you can see, it prints “n” over and over. And this goes back to my earlier example, where I mentioned that you can enter a string after the yes command and have that string printed over and over. For example:
yes Linux
Going back to our current example, we’re adding a single character to the yes command, in this case “n”, and normally it will just print that over and over. But when we pipe that command into another command, it will cause us to automatically decline the confirmation prompt, since we provided “n” as an argument (and n declines the confirmation) the command won’t finish:
yes n | sudo apt install vim-nox
Before we continue though, I wanted to point out that if a package doesn’t have any dependencies, then the behavior won’t work as you expect. To show you an example of this, I’m going to install vim-nox again:
yes | sudo apt install vim-nox
Now I’m going to remove it again, but this time I won’t remove the dependencies:
sudo apt remove vim-nox
Now watch what happens when we try and automate a “no” response with the command:
yes n | sudo apt install vim-nox
As you can see, even though we provided “n” as a response, the package still installed. The reason why the command installed the vim-nox package in this case is because there wasn’t a confirmation response. Since the dependencies for vim-nox were still installed, apt only had that one package to install. And when apt installs a single package and doesn’t need any dependencies, it will just carry out the process without a confirmation. I just wanted to point this out in case someone tries this and it still installs a package. The yes command can answer a prompt, but if there’s no prompt, then there’s nothing to answer.
yes | rm -i *.log
Next, let’s take a look at how to limit the number of responses we get. As you’ll recall, the yes command will run forever by default. But the thing is, there’s no built-in option to limit the number of times the yes command runs – it’s only able to run over and over. However, if we pipe the yes command into another command, such as the head command, it will only run that many times:
yes | head -n 5
In this case, we’re piping the yes command into the head command. Normally, the head command is used to view the first ten lines of a file. With the -n option being provided to the head command, that causes it to only show us the first 5 lines. In this case though, we’re not providing the head command with a filename.
However, by chaining the yes command into the head command, the output of the yes command becomes input to the head command. Since we’re instructing head to limit output to just 5 lines, then that causes the head command to repeat that many times, and exit. It’s a bit of a hack, but it is effective at limiting the output of the yes command.
I mentioned earlier that the yes command is often used within scripts, so I have a few bash scripts I threw together to show you how that might look like. Of course, the usefulness of the yes command depends on what you’re trying to achieve, but this simple example will give you an idea.
Now, let’s take a look at another use-case for the yes command – stress testing a system. At this point, you don’t have to follow along with me anymore, because the commands I’m going to give you might cause a server to crash. But sometimes, you’ll want to stress test a system – sometimes just to see how well the server is able to cool itself. In fact, this example will cause your systems CPU to max out. Again, not something I recommend you actually run, but the Linux installation I’m discussing in this article is just a test VM, so there’s no risk to me since I’ll be deleting this server anyway.
As I mentioned, the yes command is not going to be one that you’ll use all that often, but it’s always a good idea to keep notes regarding common Linux commands, in case you need them at some point. And the goal of the LCC series is to teach you as many commands as possible, so it was only a matter of time before I arrived at the yes command.


