Bash separation of filenames with spaces
Paul Smith
paul at mad-scientist.net
Mon Nov 18 18:57:11 UTC 2013
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. 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.
More information about the ubuntu-users
mailing list