Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does && do in redirecting an output to a file?

I found a bash script that lists all files with a blank line at the beginning or end of the file.

for f in `find . -type f`; do 
  for t in head tail; do 
    $t -1 $f  |egrep '^[  ]*$' >/dev/null && echo "blank line at the $t of $f" ;
  done; 
done

I would like to pass the output to a file. I changed the echo line to:

$t -1 $f  |egrep '^[  ]*$' >/dev/null && echo "blank line at the $t of $f" > blank_lines.log

But this didn't work.

I would like to ask what the symbol && does and why the above line does not pass the output to the file.

like image 643
Mert Nuhoglu Avatar asked Jan 30 '26 18:01

Mert Nuhoglu


1 Answers

This does not redirect output it adds another command to execute if and only if the first one succeeded. I.e command1 && command2 means execute command1 and if it is successful execute command2. Also the result of executing this statement is success if and only if both commands succeeded.

like image 155
Ivaylo Strandjev Avatar answered Feb 02 '26 09:02

Ivaylo Strandjev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!