Donglecow Donglecow - 7 months ago 25
Git Question

Why Does My BASH Script Fail When Adding Additional Arguments?

I have a BASH script to go through my Git projects recursively and pull them. We use two branches, master and development. When I change the script to include

&& git checkout development
, my script fails to run and I get this error:

find: missing argument to `-exec'

When I remove the
&& git checkout development
part again, the error remains the same even though the script is reverted to its initial state.

Here is the script:

find . -type d -name .git -exec sh -c "cd \"{}\"/../ && pwd && git pull && git status" \;

And here it is with the Git checkout call (and removing
git status

find . -type d -name .git -exec sh -c "cd \"{}\"/../ && pwd && git checkout development && git pull" \;

What is going on here? Is it possibly a red herring that the issue only occurs after changing the script?

Answer Source

Don't use placeholder {} inside command string after sh -c. You can use this find:

find . -type d -name .git -exec sh -c \
'cd "$1"/../ && pwd && git checkout develop && git pull' _ '{}' \;

Here we are passing dummy _ to populate $0 in command line and {} is being passed to populate $1 positional argument.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download