convergedtarkus
convergedtarkus

Reputation: 697

Why does using a bash variable in find fail?

I've got a find command in a bash script that works, but when I try to break it into variables that get added together it no longer works correctly.

I'm not really looking for a better way of doing this, I'd like to understand what Bash is doing in this case as I'm very stumped at this.

# Works, prints ./config
find . -type f -name 'config' ! -path './.git*'

echo
pathVar="! -path './.git*'"
# Doesn't correctly ignore './.git/config'
find . -type f -name 'config' $pathVar

echo
# Doesn't work 'find: ! -path './.git*': unknown primary or operator'
find . -type f -name 'config' "$pathVar"

Upvotes: 0

Views: 64

Answers (1)

Anubis
Anubis

Reputation: 7435

As stated in the comments,

Option 1:

cmd="find . -type f -name 'config'"
if [[<condition to run long command>]]; then
    cmd="$cmd ! -path './.git*'"
fi
eval $cmd

Option 2:

if [[<condition to run long command>]]; then
    find . -type f -name 'config' ! -path './.git*'
    # ...
else
    find . -type f -name 'config'
    # ...
fi

Upvotes: 1

Related Questions