yes
yes will continuously output lines with y until user uses ctrl+c to stop. yes Can be useful for automating
scripts.
$ yes
y
y
^C
$ yes n
n
n
n
^C
Useful Options / Examples
yes | rm -i *.txt
$ls -l *txt
-rw-r--r-- 1 apple staff 0 Mar 25 00:54 1.txt
-rw-r--r-- 1 apple staff 0 Mar 25 00:54 2.txt
-rw-r--r-- 1 apple staff 0 Mar 25 00:54 3.txt
$yes | rm -i *.txt
remove 1.txt? remove 2.txt? remove 3.txt? %
Break it down
-
Pipe
yesto the following command to automatically make the following command prove any raised question with form[y/n]. -
When using
rm -ito delete normal files, the system will ask user for confirmation of removing the file. At this time, theyescommand will be able to simplify the process of repeatedly typing in yes. Hereyes | rm -i *.txtwill continuously pipe yes to the commandrm -ito confirm the deleting operation.
yes [text] > [filename]
#!/bin/sh
yes hello >hello.txt &
PID=$!
sleep 1
kill $PID
Break it down
- The yes command is convenient to generate repeated text. We can use it in shell script like above to generate a file with repeated string
hello. Here we run theyes hellofor 1 second to generate large number ofhello. We can also useyes hello | head 1000to generatehello1000 times.