Empty curly braces and semicolon in bash

5,286

Both {} and ; have special meanings in bash but in the context of find, it is find which actually interprets these two (actually we want find to interpret these two).

First of all, -exec is an action of find. {} and ; are special parameters for the -exec predicate of find.

The syntax for the -exec action is:

-exec command ;

or

-exec command {} + 

So we need to keep {} and ; from being interpreted by shell beforehand.

In the context of find .... -exec:

  • {} indicates (contains) the result(s) from the find expression i.e. find . -name "FILE-TO-FIND" in this case. Note that empty curly braces {} have no special meaning to shell so we can get away without escaping {}

  • As bash treats ; as end of a command, we need to escape this with \ i.e. \; so that it can be parsed by -exec not by bash itself.

Share:
5,286

Related videos on Youtube

spiderface
Author by

spiderface

Updated on September 18, 2022

Comments

  • spiderface
    spiderface almost 2 years

    What does

    {} \;
    

    string mean in bash?

    I've seen it a couple of times, for example, to find and delete all files by name one must execute

    find . -name "FILE-TO-FIND" -exec rm -rf {} \;
    

    What does this string do?

  • spiderface
    spiderface over 8 years
    So what does ; do when interpreted by find?
  • heemayl
    heemayl over 8 years
    @spiderface check my edits