Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 1
    Please test your answers before posting. This will make no difference whatsoever. The problem is that $i does not contain the entire file name, each word of the name will be saved as a separate $i. You are also not quoting your variables which can cause all sorts of problems. Finally, you should also consider that not all whitespace consists of spaces. You can also have tabs, newlines etc in a filename. Commented Jan 28, 2015 at 14:42
  • They have been tested, test your comments before posting them. As you can see his code has IFS=$'\n' which makes $i get the righ name for every file SPACES INCLUDED. So the answer is perfectly right and I have tested before posting. No need to quote variables since spaces have been escaped before. Be sure when you make a comment correcting something you are right before you place the comment. Commented Jan 28, 2015 at 14:50
  • 1
    Well, the question had IFS=$'\n', your answer did not, so I didn't realize. Still, you're right, my comment was more aggressive than was necessary, sorry. Nevertheless, this will still break if the whitespace is not a simple space and your sed will only escape the first space. Commented Jan 28, 2015 at 15:16
  • You're right about multi spaces, I have added the ending 'g' to substitute all of them in case there are more than one. Commented Jan 28, 2015 at 15:21
  • Thanks, but this still breaks on tabs, newlines, carriage returns, file names beginning with - etc. It is also needlessly complicated and creates problems where there were none by parsing ls. The Right Way© is to either use shell globbing or find -print0 instead. Commented Jan 28, 2015 at 15:24