finiteloop
finiteloop

Reputation: 4496

Dealing with single quotes in file paths as returned by find

I am writing a bash script that, when run from directory B, mirrors the directory structure of directory A within directory B.

Currently, I am doing so as follows:

 #!/bin/bash          

 dirify () {
    echo $1
 }
 export -f dirify

 find "../test" -type d -exec bash -c "dirify '{}'" \;

I am running this script from directory B, and ../test is directory A. Fortunately, the directory I am using to test contains folders with ' in the name. When I run this script, bash gives the following error when it reaches those directories:

> bash: -c: line 0: unexpected EOF while looking for matching `''
> bash: -c: line 1: syntax error: unexpected end of file

(note that line 0 and line 1 refer to the lines within the dirify() function)

A more simplified way of illustrating this issue is as follows:

find "../test" -exec bash -c "echo '{}'" \;

This example produces the same errors.

Anyway, this is an issue because in production, I can't assume that file paths will not contain the ' character.

Is there anyway around this issue?

Upvotes: 3

Views: 180

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Pass it as an argument.

bash -c 'dirify "$1"' dirify {}

Upvotes: 2

Related Questions