help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Brace expansion with empty args


From: Greg Wooledge
Subject: Re: [Help-bash] Brace expansion with empty args
Date: Fri, 14 Jun 2013 13:19:51 -0400
User-agent: Mutt/1.4.2.3i

On Sat, Jun 15, 2013 at 01:49:25AM +0900, Masaki Saito wrote:
> printf is troublesome.
> 
> ~ $ echo a{,c} | od -a
> 0000000   a  sp   a   c  nl
> 0000005
> ~ $
> ~ $ echo {,c} | od -a
> 0000000   c  nl
> 0000002
> ~ $
> 
> Masaki

You're thinking of the results of a brace expansion as "bytes of output",
whereas I think of it as "a list of words".

The only time you get bytes of output is when the words are processed
as arguments and then written to stdout.  This is what echo or printf
does.  But when you're doing something like:

for f in *.{txt,csv}

then it doesn't really make sense to talk about output yet.  The results
of the brace expansion are used to generate a list of filenames to
process.

My goal in this thread was to figure out what words a brace expansion
was producing.  I personally don't have the technical knowledge to look
at the internal state of bash, so the only way I can figure it out is to
look at the bytes of output (from echo or printf) and then work backwards
to deduce what arguments were given to produce that output.  The arguments
will (hopefully) tell me what list of words the brace expansion produced.

The reason I prefer printf for this is because it allows me to put
delimiters around each word as it's written, so I can tell where
each word begins and ends.  With echo, you don't get that assistance.
Echo's output can therefore be ambiguous:

imadev:~$ echo ''{a,c}
a c
imadev:~$ echo {a\ c,}
a c

How many words were generated, and what was each word?  With echo,
you can't quite tell.  With printf, you can:

imadev:~$ printf '<%s> ' ''{a,c} ; echo
<a> <c> 
imadev:~$ printf '<%s> ' {a\ c,} ; echo
<a c> 

Now, admittedly I screwed up earlier today when I misinterpreted printf's
output of <>.  printf <%s> will produce that under *two* different
conditions: no arguments following the format specifier, or a single
null string following the format specifier.  You cannot differentiate
those two with printf, so you'll need a different approach for that
particular case.



reply via email to

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