barry
barry

Reputation: 3

Escaping Bash wildcards when executing python script from bash script?

I've got a bash script that defines an array of file globs containing * wildcards.

The script executes a python script passing each of these globs as a parameter.

On the command line I can quote the entire glob, and python is happy with it.

In the bash script, if I quote the globs, python's os.path.realpath seems to get confused with the quotes - which are passed in to the script itself.

e.g. bash script:

BACKUP_PATHS=("/root/backups/db-dump-*" "/root/backups/*_backup.tar.bz2")
for path in "${BACKUP_PATHS[@]}"
do
  /usr/local/bin/python2.7 /usr/local/bin/clean.py \'$path\' $MAX_FILES
done

python:

os.path.realpath(path)    # results in something like: /usr/local/bin'/root/backups/db-dump-*'

How can I make them play together?

Thanks

Upvotes: 0

Views: 511

Answers (1)

interjay
interjay

Reputation: 110108

Instead of \'$path\', use "$path". The double-quotes allow the contents of the variable $path to be substituted, but prevent wildcard expansion and ensure the path is passed as a single argument.

Upvotes: 2

Related Questions