Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Script - store find command output without spaces in variable

Tags:

bash

I am looking to automate my xcode projects. It all works fine except the projects name with spaces. I have tried the following commands:

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

When I run

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

without storing into variable, it gives me output as expected as mentioned below:

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

However when I store the output of above command in a variable as below

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

and then run the following for loop for get the names

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

It shows

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

How do I maintain the spaces between each output and get the following output?

Ten EU.app
Ten Official App EU.app
Ten Official App.app
Ten.app
like image 422
Vebz Avatar asked Jan 09 '13 23:01

Vebz


People also ask

How do I remove spaces from a variable in bash?

${var/ /} removes the first space character. ${var// /} removes all space characters. There's no way to trim just leading and trailing whitespace with only this construct.

How can I store the find command results as an array in bash?

In bash, $(<any_shell_cmd>) helps to run a command and capture the output. Passing this to IFS with \n as delimiter helps to convert that to an array. Benjamin W. This will get only the first file of the results of find into the array.

What does $@ do in bash script?

Symbol: $# The symbol $# is used to retrieve the length or the number of arguments passed via the command line. When the symbol $@ or simply $1, $2, etc., is used, we ask for command-line input and store their values in a variable.

How do you store output of find command in variable in UNIX?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]


1 Answers

If you don't need to store the file names in a variable, you can use find -print0 in combination with xargs -0. This separates the found entries by NUL bytes instead of newlines. xargs reads these NUL separated 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 to some-command with xargs -n 1

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

Yet another approach is to read the files with a while loop

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

This calls some command with one file at a time. The important point is to enclose the $f into double quotes.

like image 137
Olaf Dietsche Avatar answered Sep 26 '22 23:09

Olaf Dietsche