Andranik
Andranik

Reputation: 2849

How to escape space in file path in a bash script

I have a bash script which needs to go through files in a directory in an iOS device and remove files one by one. To list files from command line I use the following command:

ios-deploy --id UUID --bundle_id BUNDLE -l | grep Documents

and to go one by one on each file I use the following for loop in my script

for line in $(ios-deploy --id UUID --bundle_id BUNDLE -l | grep Documents); do
     echo "${line}"
done

Now the problem is that there are files which names have spaces in them, and in such cases the for loop treats them as 2 separate lines.

How can I escape that whitespace in for loop definition so that I get one line per each file?

Upvotes: 1

Views: 1035

Answers (1)

Kubator
Kubator

Reputation: 1383

This might solve your issue:

while IFS= read -r -d $'\n'
do
  echo "${REPLY}"
done < <(ios-deploy --id UUID --bundle_id BUNDLE -l | grep Documents)

Edit per Charles Duffy recommendation:

while IFS= read -r line
do
  echo "${line}"
done < <(ios-deploy --id UUID --bundle_id BUNDLE -l | grep Documents)

Upvotes: 2

Related Questions