RashaMatt
RashaMatt

Reputation: 252

(bash) flock: when to use the -c option?

Can anyone explain to me why the -c option exists in flock?

I can't find a good description of how it differs from simply specifying the command(s) to execute after flock (apart from its limitation of no arguments to the command).

Upvotes: 1

Views: 674

Answers (1)

that other guy
that other guy

Reputation: 123570

-c invokes a shell with the command.

Consider this:

flock .lock somecommand > myfile

Since > is interpretted by the current shell and not flock, myfile will be truncated before the lock is captured.

You can work around this with -c:

flock .lock -c 'somecommand > myfile'

Now the redirection is performed after the lock is captured. However, it is indeed useless since you could just have invoked a shell yourself:

flock .lock sh -c 'somecommand > myfile'

Upvotes: 3

Related Questions