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

Re: bash and variable holding directories with spaces

On Sun, Dec 04, 2005 at 12:11:22AM +0100, Almut Behrens wrote:
> On Sat, Dec 03, 2005 at 05:58:28PM -0500, H.S. wrote:
> > $ DIRS="'file\ 1 file\ 2'"; ls -ln "$DIRS"
> > ls: 'file\ 1 file\ 2': No such file or directory
> in this case you probably want
> $ DIRS='file\ 1 file\ 2';  eval ls -ln $DIRS

I'm not sure quite what the requirements are here, but although this
works it would probably be more natural either to use perl or to use an
array.  For a shell script, this would be more appropriate, being both
portable and straightforward:

set "file 1" "file 2"
ls -ln "$@"

Of course, this clobbers the "$@" array.  Better shell script style is
to arrange your code to allow something like:

ls_ln() { ls -ln "$@"; }
ls_ln "file 1" "file 2"

You could use a non-portable bash array:

DIRS=("file 1" "file 2")
ls -ln "${DIRS[@]}"

Seriously though, shell scripting sucks.  Perl!  It's on every debian
system with debconf.

Reply to: