Syntax
You can use `yes` command with an option or any string value, but both are optional for this command.
yes [OPTION]
yes [STRING]…
Options
This command has not more options. Two options of this command are mentioned below.
–version
It is used to display the installed version of this command.
–help
It is used to get detail information of this command.
Example#1:
When you run the `yes` command without any option and string value then it will print ‘y’ for infinite times.
Output:
The following output will appear.
Example#2:
When you run the `yes` command with a specific string value then it will print the string value for infinite times.
Output:
The following output will appear.
Example#3:
`cp` command is used in bash to create any new file by copying an existing file. If the new filename exists then it will ask for overwrite permission if you run cp command with -i option. In this example, two text files hello.txt and sample.txt are used. If these two text files exist in the current location and `cp` command is run for copying sample.txt to hello.txt with -i option then it will ask for overwrite permission.
$ cat sample.txt
$ cp -i sample.txt hello.txt
You can use `yes` command to prevent from overwriting the existing file or forcefully overwrite the existing file. In the following commands, the first command is used to prevent the overwrite and the second command is used to overwrite the file without any permission.
$ yes | cp -i sample.txt hello.txt
Output:
Example#4
You can use `yes` command to run any script multiple times in the command line. In this example, `yes` command is used to run while loop repeatedly ten times. Here, `yes` command will continuously send the numeric value from 1 to 10 to the loop and the loop will print the values in regular interval of one second.
Output:
Example#5:
You can use `yes` command to send any string value to a script while executing the script file. Create a bash file named ‘yes_script.sh’ and add the following script. If you run the script using `yes` command with empty string then it will print “Empty value is passed by yes command” otherwise it will print the string value send by `yes` command by combining with other string.
#Read the value passed from yes command
read string
#check the string value is empty or not
if [ "$string" == "" ]; then
echo "Empty value is passed by yes command"
else
newstr="The value passed by yes command is $string"
echo $newstr
fi
Run the `yes` command with an empty string and the bash script file, yes_script.sh.
Output:
Run the yes command with a string value, “testing” and the bash script file, yes_script.sh.
Output:
Example#6:
You can use `yes` command for the testing purpose also. You can run the following command to create a file with a huge amount of data for testing. After executing the command, a file named ‘testfile’ will be created that will contain 50 lines with the content, ‘Add this line for testing’.
Output:
Conclusion
The basic uses of `yes` command are shown in this tutorial by using different types of examples. It is a very useful command when you are confirmed about any task and don’t want to waste time for unnecessary confirmation. You can use this command for some advanced level tasks, such as comparing processors ability or the loading capacity of any computer system etc.