You are not logged in.

#1 2025-01-31 02:00:39

TankieTanuki
Member
Registered: 2024-11-21
Posts: 5

[SOLVED] What's the difference between "... | sync" and "... && sync"?

Examples:

dd if=/dev/foo of=/dev/bar | sync
dd if=/dev/foo of=/dev/bar && sync

Last edited by TankieTanuki (2025-01-31 04:12:17)

Offline

#2 2025-01-31 02:42:03

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,623

Re: [SOLVED] What's the difference between "... | sync" and "... && sync"?

Did you mean || rather than |   ?

A single | means take the output of one command and use it as the input to the second command.  Your example of dd with an input file and and output file does not generate any useful output and sync does not take any input.

Two bars ( || ) means 'Or'.  If the first command or the second command are successful, then the combination is successful.  The side effect is the first command is run.  If it is successful, there is no need to run the second command since the first already succeeded.  So,

dd if=/dev/foo of=/dev/bar || sync

would run the dd command and, if it worked, would not run the sync command because it does not have to, the 'or' clause is already guaranteed to pass.

&& means 'And'.   Both commands have to be successful for the combination to be successful.  So,

dd if=/dev/foo of=/dev/bar && sync

will run the dd command, and if it is successful will also run the sync command.  If it is successful, then the combination is successful.  On the other hand, if the dd command failed, then sync will never be executed because Bash already knows they cannot both be successful since the first one was not, so why run the second command?

There is also ;; which says run the both regardless of whether the first one worked or not.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#3 2025-01-31 03:58:58

TankieTanuki
Member
Registered: 2024-11-21
Posts: 5

Re: [SOLVED] What's the difference between "... | sync" and "... && sync"?

Yes, I meant the pipe. I must have seen that usage somewhere and picked it up without thinking critically. Thank you for setting me straight!

Now that I know that dd has no standard output, the ; command separator makes the most sense (it is a single semicolon, isn't it?).

---

I'm curious what was actually happening when I ran it the piped way. I'm guessing that sync would run at the start of the dd command rather than the end, defeating its purpose in this context?

Offline

Board footer

Powered by FluxBB