Can anyone explain to me why the -c option exists in flock?
-c
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).
-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.
>
myfile
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'
Copyright © 2021 Jogjafile Inc.
-c
invokes a shell with the command.Consider this:
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
:Now the redirection is performed after the lock is captured. However, it is indeed useless since you could just have invoked a shell yourself: