bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Spaces in File name Handling


From: alupu
Subject: Re: Spaces in File name Handling
Date: Thu, 11 Mar 2004 19:06:03 +0000

Hi Paul,

> Alex asks (excerpt):
This (and variations) does not work:
TMP=`ls -AQ`
for i in "$TMP" ; do ls -ld "$i" ; done
---
>> Paul replies (excerpt):
Don't quote $TMP, and make sure it gets split only at newlines, not [at any] 
other whitespace:
TMP=`ls -A`
IFS=$'\n'
---
THANK YOU VERY MUCH!

At your leisure, I'd very much appreciate your thoughts on this:

The “clincher” was the “IFS=$'\'” (I had dabbled with TMP without quotes, ls 
without Q, etc.).  I thought I'd seen Unix before but I'm still reeling after 
finally realizing that the output of the good old “list” command, ls, has – 
behind the scenes - the file names separated by LF and not by spaces as it 
appears on the screen of the unsuspecting (and uninitiated) user.
Based on your solution for me:

# In a directory with three files, “A”, “B C” and this “T” procedure:
TMP=`ls -A`
echo $TMP > E
IFS=$'\n'
for i in $TMP ; do ls "$i" ; done

# Output of T:
A
B C
T

# Dump of E:
41 20 42 20 43 20 54 0a

# Screen output of an “ls” command:
A B C T
# (i.e. NO line feeds – the default, for conciseness I suppose)

ls > G
# Dump of G
41 0a 42 20 43 0a 54 0a
# (here, the file names are clearly demarcated by LFs – the basis of your 
beautiful solution)

PUZZLE:
When the shell hands over an argument ($TMP) to “echo” it removes the LFs while 
in the “for” construct it doesn't touch them.
Does this go all the way back to the old “Doug McIlroy” philosophy on echo 
described in “The Unix Programming Environment” - Using the Shell, by BWK and 
RP?

Thanks again and best wishes,
-- Alex




reply via email to

[Prev in Thread] Current Thread [Next in Thread]