Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 690
finiteloop Avatar asked Dec 06 '25 15:12

finiteloop


1 Answers

Pass it as an argument.

bash -c 'dirify "$1"' dirify {}
like image 161
Ignacio Vazquez-Abrams Avatar answered Dec 08 '25 09:12

Ignacio Vazquez-Abrams