Bash “for” loop breaks at spaces in file namesPosted: April 2, 2014
Say, you want to batch create a set of soft links to files.
The first example will not work, as directory and file names with spaces in them will be broken into separate words:
currdir=`pwd`; for i in `ls *.mpg` ; do ln -s $currdir/$i /tmp/destinationdir; done
This example will work just fine:
currdir=`pwd`; for i in *.mpg ; do ln -s "$currdir"/"$i" /tmp/destinationdir; done
- Do not use ls or find to get the file names, instead use the bash shell’s built-in functions!
- Enclose the variable names in double quotes!