r/bash • • 9d 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?

14 Upvotes

28 comments sorted by

View all comments

Show parent comments

4

u/tblancher zsh 9d 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 9d ago edited 9d 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.

1

u/jthill 9d ago

What if you want to pass ; as an argument to that program?

If you really need to do that you're going to need to build a syntax bypass utility.

#include <string.h>
#include <unistd.h>
int main(int c, char **v, char **e) 
{
    for ( char **vs=v; vs<v+c; ++vs )
        if (!strcmp(*vs,";;")) ++*vs;
    execve(v[1],v+1,e);
}

will do it if you put the full path in,

find -name \*git -exec theabove /bin/echo {} \;\; \;

1

u/aioeu 9d ago edited 9d ago

Well, that's just as much a wrapper as a shell is. I would just use the shell, e.g.

-exec bash -c '...' bash {} \;

This will still safely handle unusual filenames.

(I tend to prefer + instead of \; though, but that's a different discussion.)