Assuming $file
holding a value of a file name, say Dr' A.tif
. In bash programming, how could I escape single quote and any other special character of the $file
without removing the special character?
Update on 9 July 2014
As request from @Gilles, following code snippet that doesn't able to handle Dr' A.tif
:
files=$(find /path/ -maxdepth 1 -name "*.[Pp][Dd][Ff]" -o -name "*.[Tt][Ii][Ff]")
echo "${files}" > ${TEMP_FILE}
while read file
do
newfile=$(echo "${file}" | sed 's, ,\\ ,g') ## line 1
done < ${TEMP_FILE}
After I have tried out the answer from @Patrick on line 1
, it seems to work for me. But if I have file such as Dr\^s A.tif
, printf
command doesn't seem help, it shows me Dr\^s\ A.tif
. If I manually try it on console like this:
printf "%q" "Dr\^s A.tif"
I will have this output:
Dr\\\^s\ A.tif
Any idea how to handle this?