Pages

Tuesday, October 8, 2013

Handling Spaces in BASH Shell Scripting

Handling Spaces in BASH Shell Scripting:

I had a requirement to list contents of a directory and display their file types.
Files in the directory may contain spaces.I tried with the following code list.

for i in `ls`
do
file $i
done

Everything works fine, but if there exists any spaces in the file names, this script will not work.

Searching through forums I got the following link,

http://stackoverflow.com/questions/3967707/file-names-with-spaces-in-bash

The Same script is just modifed as follows:

for i in *
do
file "$i"
done

At last  this script worked without any problems !

No comments:

Post a Comment