This question already has an answer here:
let's say I have a directory with the following files/directories:
google apple mozilla foundation # a file with spaces browsers
So I would like to move files into
browsers directory. Here is a script I wrote:
for d in $(ls); do if ! [ "$d" == "browsers" ]; then mv "$d" "browsers" fi done
apple went to
browsers but I got:
mv: cannot stat 'mozilla': No such file or directory mv: cannot stat 'foundation': No such file or directory
Obviously, the problem is spaces in a variable. What's the proper way to deal with this?
Of course I believe there should be a more elegant one-line command to do this, but I would like to know how I should use variables with spaces.