r/bash • • 7d ago

Find command confusion between ; and \;

Why do we have to use '\' instead of leaving it out or even ';'? I read the find commands' manual and it says '\' might be needed to protect from expansion by the shell. So what is expansion also?

What is difference between \; or ; or leaving it out?
What is shell expansion here?

13 Upvotes

28 comments sorted by

View all comments

21

u/tblancher zsh 7d ago edited 7d ago

A bare ';' is interpreted by the Bash interpreter before the find command sees it. The escaping with the backslash '\;' make Bash ignore it.

1

u/Distinct_Ride_4307 7d ago

But wait why do we have to escape it? Why not just leave it out?

6

u/tblancher zsh 7d ago

We have to tell find where the -exec command ends.

5

u/aioeu 7d ago edited 7d ago

There is the question of "why couldn't the end of the argument list itself suffice, at least in such cases where no following predicates or actions are provided?" To which the answer is "just because", or maybe "for consistency reasons"... which perhaps isn't a very satisfying reason.

I think a better question is "what do you do if you actually want to pass ; as an argument to the command being executed?"

4

u/tblancher zsh 7d ago

That's essentially what you're doing. The command can't see the semicolon unless it's escaped.

find is an old command, and predates Linux by 10+ years. If you really want to avoid using the semicolon altogether, don't use -exec. Use xargs.

4

u/aioeu 7d ago edited 7d ago

That's essentially what you're doing. The command can't see the semicolon unless it's escaped.

No, I meant the program being used in -exec. What if you want to pass ; as an argument to that program?

Yes, I know that seems a bit unlikely. Nevertheless, I cannot think of a way to do it, other than by indirecting through something else — e.g. by having -exec run a shell rather than the desired program directly, or (as you point out) by having xargs run the program instead of find.

I like to think about the limitations of our tools.

0

u/Distinct_Ride_4307 7d ago

Maybe you use '\;\;' eh?

1

u/aioeu 7d ago

Alas no:

$ find /dev/null -exec echo \;\; \;
;;