Bash yes Command

29/12/2020
Bash `yes` command is one of those commands of Linux that is related to the operation of another command. Using this command is useless when you execute the command independently. By default, `yes` command repeats the character ‘y’ if no string value is specified with this command. When `yes` command uses with pipe and another command then it will send the value ‘y’ or `yes` for any confirmation prompt. This command can help to save time by doing many confirmation tasks automatically.

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.

$ yes

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.

$ yes test

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 hello.txt
$ 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 n | cp -i sample.txt hello.txt
$ 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.

$ yes "$(seq 1 10)" | while read n; do  echo $n; sleep 1; done

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.

#!/bin/bash
#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.

$ yes "" | bash yes_script.sh

Output:

Run the yes command with a string value, “testing” and the bash script file, yes_script.sh.

$ yes testing | bash 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’.

$ yes ‘Add this line for testing’ | head -50 > testfile

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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash pipe tutorial

Your instructor says, “If you put this vertical bar after a command it will pass the message on to the next.” So, you...
29/12/2020

Bash exit on error

An exit status code is returned when any Linux command is executed from the terminal, either the command is successful...
29/12/2020

How to Set Environment Variables in Linux

Setting environment variables in Linux is a good way to define common and repetitive variables that are used across a number...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024