[Top][All Lists]

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

Re: Encoding multiple filenames in a single variable

From: Greg Wooledge
Subject: Re: Encoding multiple filenames in a single variable
Date: Mon, 30 Aug 2010 09:07:53 -0400
User-agent: Mutt/

On Sun, Aug 29, 2010 at 04:07:23AM -0400, Chris F.A. Johnson wrote:
> On Sun, 29 Aug 2010, Jon Seymour wrote:
> >This isn't strictly a bash question, and I'd prefer a POSIX-only
> >solution if possible

> >Suppose I need to encode a list of filenames in a variable

POSIX shells won't have arrays (they're allowed to, but they're also
allowed NOT to, which means you can't count on them being present), but
you can enlist the positional parameters for use as an array under
certain conditions.

  set ./*myglob*
  process "$@"

Of course you don't get bash's full set of capabilities.  You can't
use range notation to get a "slice" (as some people call it) of an array
(for the rest of us, that means "from element M to element N"), and
you can't set or unset individual elements.  Nor can you populate it
using a loop reading from "find -print0" or similar as you can with a
bash array.

But if you just need to populate them from a glob, this should suffice.

>    Either separate them with newlines, or (non-POSIX) use an array.

Filenames can also contain newlines, unfortunately.

A third choice would be to store the list of filenames in a file, rather
than in a variable.  The advantage of this is that you can store NUL
bytes in a file (unlike in a variable).  The disadvantage is a need for
platform-specific utilities to create safe temporary files, or for some
sort of application-level strategy to earmark a safe place to create them
using primitive-but-portable means.  And of course you'd need some way
to read them from the file.

It all boils down to exactly what you need to do with the filenames once
you have them.

reply via email to

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