LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Spaces in arguments in a bash for loop

[ILUG] Spaces in arguments in a bash for loop

Niall O Broin niall at magicgoeshere.com
Fri Nov 3 11:20:33 GMT 2000


While looking into my Dingbats fonts problem (q.v.) I encountered another
problem about which I've often meant to ask. An example may be best

niall at bagend:/tmp >grep ding fonts|grep -v TT
-ttf-webdings-medium-r-normal-regular-0-0-0-0-p-0-iso8859-1
-ttf-wingdings-medium-r-normal-regular-0-0-0-0-p-0-iso8859-1
-ttf-zapfdingbats bt-medium-r-normal-regular-0-0-0-0-p-0-iso8859-1


niall at bagend:/tmp >for dingfont in `grep ding fonts|grep -v TT`
> do echo $dingfont
> done
-ttf-webdings-medium-r-normal-regular-0-0-0-0-p-0-iso8859-1
-ttf-wingdings-medium-r-normal-regular-0-0-0-0-p-0-iso8859-1
-ttf-zapfdingbats
bt-medium-r-normal-regular-0-0-0-0-p-0-iso8859-1


As you can see, in the second command, where I used the first command's
output in a substitution, I get four lines of output, although I
only expect three. I know WHY this is happening (the space in the third
output line is being seen as a separator, hence dingfont gets four different
values rather than three) but I'd like to know how to stop it happening. I
regularly have to deal with files with embedded spaces in their names, and
some of what I need to do would be done nicely in a for loop as above, but
the embedded spaces screw things up for me. Is there a neat way of solving
this problem ? I say neat because one way which occurs to me is this

niall at bagend:/tmp >for x in `grep ding fonts|grep -v TT|sed -e 's/ /*/g'`; do
echo `echo $x|sed -e 's/*/ /g'`; done
-ttf-webdings-medium-r-normal-regular-0-0-0-0-p-0-iso8859-1
-ttf-wingdings-medium-r-normal-regular-0-0-0-0-p-0-iso8859-1
-ttf-zapfdingbats bt-medium-r-normal-regular-0-0-0-0-p-0-iso8859-1

but that sucks large hairy rocks (though I must say that I only just thought
of it, and I'll use it in future unless one of you geniuses can come up with
the 'right' answer)

The bash man page is rather unhelpful. It says

Bash performs the expansion by executing command and replacing the command
substitution with the standard output of the command, with any trailing
newlines deleted. Embedded newlines are not deleted, but they may be removed
during word splitting.

As I see it, embedded newlines ARE deleted. Perhaps they are "removed during
word splitting" but what does that mean exactly ? When I do 

echo `grep ding fonts|grep -v TT` > xx

I can't see that I'm doing any word splitting, and yet the resultant file xx
only has one line.

BTW the result's the same whether I use ` substitution or the new $() style.



Regards,


Niall




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell