0

I am reading this snippet for using flock:

exec {lock_fd}>/var/lock/mylockfile || exit 1
flock -n "$lock_fd" || { echo "ERROR: flock() failed." >&2; exit 1; }
 
# ... commands executed under lock ...
 
flock -u "$lock_fd"

And I have no idea what does {lock_fd} means there. I have tried to run this in my shell, and echo its value out repetitively:

~/learning-material/shell master*
❯ exec {lock_fd}>test.lock

~/learning-material/shell master*
❯ echo $lock_fd
18

~/learning-material/shell master*
❯ exec {lock_fd}>test.lock

~/learning-material/shell master*
❯ echo $lock_fd
19

What does that mean? What is the syntax related with {lock_fd}? My guess right now is that is a file descriptor, or a unique ID that will be increased automatically.

John Winston
  • 1,260
  • 15
  • 30
  • I've edited the answer on the linked duplicate to be very explicit about the meaning of this syntax. As you surmised, it's automatic file descriptor allocation. – Charles Duffy Sep 29 '21 at 18:53
  • curve brackets is just to group several command into one block. Block should be finished with ; – Saboteur Sep 29 '21 at 22:14

0 Answers0