-   Programming (
-   -   Shell Scripting and Files With Spaces (

Matir 08-12-2005 01:25 PM

Shell Scripting and Files With Spaces
A little tip:

If you're writing a shell script that's going to use filenames that have spaces in them, and you need to iterate through them, you may find that bash splits the files in the wrong place. For example, if I have a file called "A file with spaces", bash may try to treat that as 4 files.

So, here's the trick... use the -Q option to ls. It automatically quotes your filenames for you.

for file in `ls -Q`
    do echo $file

microsoft/linux 08-12-2005 09:49 PM

i believe you can also write it as

A\ file\ with\ spaces
treating the ' ' as a special character in the filename, the same way you would with $,etc

addy86 08-13-2005 02:16 AM

You can, but that won't solve the problem with ls.

Matir 08-13-2005 09:14 AM

Yeah, this wasn't supposed to be "everything", but it was supposed to be an easy way when dealing with ls. :)

microsoft/linux 08-13-2005 12:50 PM

oops, my bad. Sorry to be off topic:)

Matir 08-13-2005 02:01 PM

microsoft/linux: no, it's cool, it was very much related, but it just addresses two separate needs. I just discovered the -Q and --quote-style options to ls and thought it was worth sharing, with all the troubles I've seen on here with filename splitting.

eddiebaby1023 08-13-2005 03:10 PM

A neat trick indeed, but if it's GNU-specific your script won't be portable to UNIX systems. Better would be

ls | while read name
    echo "$name"

Matir 08-13-2005 03:26 PM

Running your script gives:

$ touch "This is a test file"
 $ touch "This is also a test file"
 $ ./
This is a test file
This is also a test file

Running it with it modified to put backslashes in front of the quotes gives:

$ ./
"This is a test file"
"This is also a test file"

Which is, surprisingly, intended behavior. My interpretation of the read man page was incorrect. :)

eddiebaby1023 08-14-2005 08:07 AM

I put quotes around the echo()ed argument to preserve the whitespace in the filenames. Try the two variations with more than a single space (or tabs) between the words in the filename and see the difference.

julian_s 08-15-2005 05:04 PM

I find this topic doing my first ever script, and I still haven't found how to solve it, here is what I did, based on what you said:


for file in `ls -Q $1 | grep .m3u`
        echo "$1""$file"

And this is the output:


And the correct ouput will be

/home/julian/"how to dismantel an atomic bomb.m3u"
/home/julian/"Tyrannosaurus hives.m3u
So what do you suggest??

julian_s 08-15-2005 06:21 PM

Never mind, I found another way:



for fic in $(ls); do
        echo $fic


enemorales 08-16-2005 06:16 AM

I think that


for i in *.m3u; do ...
also works. Does't it?

Matir 08-17-2005 01:43 AM

enemorales: try that with files containing spaces in the names?

All times are GMT -5. The time now is 06:03 PM.