Bash Script - save search command output without spaces in a variable

I want to automate my xcode projects. Everything works fine except for the name of the projects with spaces. I tried the following commands:

output_apps=`find ./ -name "*.app" -print` output_apps=`find ./ -name "*.app"` 

When i started

 find ./ -name "*.app" -print 

without saving to a variable, it gives me the result as expected, as follows:

 .//Ten EU.app .//Ten Official App EU.app .//Ten Official App.app .//Ten.app 

However, when I save the output of the command above in a variable as shown below

 output_apps=`find ./ -name "*.app" -print` 

and then run the for loop to get the names

 for curr_app in $o do echo "$curr_app" done 

He shows

 .//Ten EU.app .//Ten Official App EU.app .//Ten Official App.app .//Ten.app 

How to keep spaces between each output and get the next output?

 Ten EU.app Ten Official App EU.app Ten Official App.app Ten.app 
+4
source share
2 answers

If you do not need to store the file names in a variable, you can use find -print0 in combination with xargs -0 . This separates the found entries using NUL bytes instead of newlines. xargs reads these NUL divided values ​​and calls some command with as many arguments as possible.

 find ./ -name "*.app" -print0 | xargs -0 some-command 

If you want, you can limit the number of arguments given by some-command with xargs -n 1

 find ./ -name "*.app" -print0 | xargs -0 -n 1 some-command 

Another approach is to read files with a while

 find ./ -name "*.app" -print | while read f; do some-command "$f" done 

This calls some command with one file at a time. An important point is the inclusion of $f in double quotes.

+13
source

File names may contain spaces. You need to ask to find them for separation through NULL(\0) . Use find -print0 .

+1
source

All Articles