Submind YouTube summaries
Thumbnail for 'yes' - output a string repeatedly - Video Man Pages

'yes' - output a string repeatedly - Video Man Pages

Watch on YouTube

Video summary

The `yes` command is a utility included in the standard GNU core utils package designed to continuously output a specific string until it is manually terminated. By default, when executed without any additional arguments, it repeatedly prints the letter "Y" to the terminal. This behavior is particularly useful for automating interactions with programs or scripts that require repeated confirmation inputs, such as those asking yes-or-no questions. For instance, when using package managers like `pacman` on Arch Linux or `apt` on Debian-based systems, users often encounter prompts regarding file conflicts or repository updates. By piping the `yes` command into these installation commands, the system can automatically answer "yes" to every prompt, streamlining the process without manual intervention. Another frequent application of this tool involves managing file deletion operations using the `rm` command with interactive flags. When removing multiple files individually, the shell may pause to ask for confirmation for each one, which can be tedious if the user intends to delete everything. In such scenarios, running `yes` allows the user to bypass these individual prompts by automatically providing affirmative answers for every file listed. Beyond automation, the command also serves as a method for stress testing system resources. Users can direct the continuous output of `yes` into `/dev/null` and run it in the background using an ampersand to simulate high CPU load without cluttering the terminal screen with unnecessary text. The flexibility of the `yes` command extends beyond its default output, allowing users to specify any custom string they wish to repeat. This feature is essential when a script or program expects a specific response other than "Y," such as an empty line representing the Enter key. To achieve this, one can pipe an empty string enclosed in single quotes into the command, which effectively simulates pressing Enter repeatedly. Once the desired task is complete or if the background process becomes too resource-intensive, it can be cleanly stopped using standard process management tools like `pkill` by name or `kill` by process ID. Overall, despite its simplicity and lack of complex flags, `yes` remains a powerful utility for automating repetitive input tasks and testing system performance.
Read the full video transcript
The yes command is part of the standard GNU core utils package. What does the yes command do? Well, it outputs a string repeatedly over and over again until you kill the command. Let me show you exactly what I'm talking about here. If I type yes and I don't give it any other flags or options, what this will do is it will continuously output Y to the terminal over and over and over again until I kill the command. If I type control C, it will kill that process. Let me clear that. Now, what is the purpose of this? Why have something output Y over and over again? It's because a lot of times terminal commands and sometimes scripts, sometimes programs ask you for information. They prompt you typically for a yes or no question where Y is yes, N is no. And in this case, if I use the yes command, right, I can continuously over and over again answer Y to all questions that come up in a particular script or program. For example, if I ran a sudo pacman {dash} capital S lowercase Y U here on an Arch Linux machine, what would happen is sometimes it's going to ask me, "Hey, do you actually want to do this? Yes or no?" Meaning maybe there's a program that is no longer in the repos or it's being conflicts with another program. "Hey, do you want to have this program override another program?" Whatever it might be. You know, it'll ask some yes or no questions. And if I just want to answer yes to everything, which can be dangerous. I don't necessarily recommend that, but I could pipe yes into this sudo pacman {dash} capital S Y U command and it will simply automatically answer Y to every yes or no question that comes up. That way I don't have to. And naturally, that would work with all of your package manager. So, if you're on, you know, an Ubuntu or a Debian and you want to answer, you know, something with a like the apt get command or whatever, apt get install, you know, name of program, you know, when you want to answer yes in case you get a prompt or an update command or whatever it is, just use yes. Yes pipe and then the command. One of the most common reasons I will use the yes command is sometimes you will have a situation where you're constantly being asked yes or no about removing files. The RM command, you know, maybe you're RM file one, file two, file three. And for me, I have my RM command alias to always use the interactive flag. So, my RM command is actually RM-i for the interactive mode, meaning that hey, if this file exists, prompt me yes or no before you delete it, right? And so, it's going to ask me three different yes or no questions, one for file one, one for file two, one for file three if they exist on the system. Well, I would just answer yes right away if I know I'm going to answer yes to all of those remove prompts. Another interesting use for the yes command, you will sometimes see people use yes to test the the load on their computer, the CPU load. For example, I could type yes and then use the right pointing chevron, the greater than sign, and maybe direct all the output over to /dev/null and then give it the ampersand at the end. What this does is it's going to output y over and over again, over and over again, right? Because that's the default yes functionality. Except I'm not going to have that output to the terminal, I don't need to see it. I'm just going to have that output directed over to /dev/null. That that's just a place on the system where information just goes to die, all right? But it's that process will be running over and over and over again on my machine, and I could test, you know, the CPU load on the machine if I really wanted to. If I can run that command, and it's running, right? That yes command is running right now. Now, how to kill that yes command that's running in the background? Well, I could use the pkill command, I could pkill yes, for example, and you can see that job one yes was terminated and you can actually see it even gave me the full command there. That was job one yes directed over into /dev/null. Another thing I could have done, I could have also just done a kill PID as long as I had the process ID of that process. Now yes doesn't necessarily have to always output a Y. You can actually specify the string that you want yes to output. For example, if I want yes to output the word string, it's just going to output that over and over and over again. That way, no matter what the prompt is, whatever you're trying to always answer the question with, you know, you can get that. For example, there are many programs or shell scripts sometimes that will ask for a prompt and the confirmation for the prompt is for you to hit enter. Well, yes by itself is not going to work because that's not the enter key, right? That's Y. So, how would you do something for a a shell script or a program that needed a enter? Well, you would do yes and then how about single quotes? One single quote and then another single quote with nothing in between, so an empty string basically. And if I hit enter, that's just printing a new line over and over again, which is essentially hitting enter over and over again. So, that is useful to automate installations or scripts that repeatedly ask you for enter. And let me kill that process and that is essentially it for the yes command. A very simple command, there's not a lot of flags or options to it. If you want to learn more about yes, type man yes in the terminal to read the man page.