Bash separation of filenames with spaces

Johnny Rosenberg gurus.knugum at gmail.com
Fri Nov 22 23:32:21 UTC 2013


2013/11/18 Paul Smith <paul at mad-scientist.net>

> On Mon, 2013-11-18 at 19:49 +0100, Johnny Rosenberg wrote:
> > tried to simulate the quotes, but it seems like those becomes a part
> > of the file names:
>
> > OldIFS="$IFS"
> > IFS=$'\n'
> >
> > FileList=""
> > for FileName in $@; do
> >      FileList="${FileList}"'"'"${FileName}"'"'" "
> > done
> > IFS="$OldIFS"
>
> > SomeCommand --options $FileList
> > # End of code
>
> You've not been clear about exactly what you want to do, from a higher
> level.


No, I try to avoid that these days, because when I give away every detail I
always get suggestions about things I didn't ask about. Like why don't you
install this and that instead or whatever. Most people don't seem to
understand that there are people like me around, who just want to learn
things rather than only finding the ”best” way to get the ”job” done…


>  Do you mean you want to write a script that can take filenames
> as arguments, some of which may contain whitespace, and you want to pass
> those arguments to another command without losing the spaces?
>
> You don't have to do all that mess up there.  Just use "$@" (quotes must
> be included!!  Very important):
>
>   #!/bin/sh
>   SomeCommand --options "$@"
>
> Done.  This is part of POSIX sh and has been true since forever.  See
> the special handling of "$@" in the shell man pages.
>

He he he… yes, of course. A real beginner's mistake! I guess I was blind
there. I always use quotes when spaces is involved, but for some reason I
seem to have forgotten them this time in the for–in-statement. I guess I
was staring at the code for too long or something…

Thanks for opening my eyes! I really appreciate it. Most people probably
thought that it was just a too stupid question and thought things like ”oh
my, does that guy never learn…?”, but you took the time!


Regards
Johnny Rosenberg


>
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20131123/ba8d1831/attachment.html>


More information about the ubuntu-users mailing list