P Shaw P Shaw - 14 days ago 7
Bash Question

printf making new line on space

I have the following printf code, it puts a newline in everytime it hits a space in my files. How do I stop it adding in this newline.

File list:

mytest
mytest stuff
new test things


Code:

for i in `find / -iname *test*`; do
printf $i'\n'; done


Output:

/var/private/mytest
/var/private/mytest
stuff
/var/private/new
test
things


I am trying to strip out the space, and put a newline after each file:

/var/private/mytest
/var/private/myteststuff
/var/private/newtestthings

Answer

find here is only being used to implement something you can do in pure bash (assuming version 4 or later):

shopt -s nocaseglob globstar nullglob
for i in /**/*test*; do
    path=${i%/*}
    base=${i#$path/}
    printf '%s\n' "$path" "${base// /}"
done