Bash separation of filenames with spaces
Johnny Rosenberg
gurus.knugum at gmail.com
Mon Nov 18 18:49:36 UTC 2013
In a Bash script I need to run a command with a list of file names. If I
enter the command manually in a terminal, it could look something like this:
SomeCommand --options "A File.x" "Another File.x" "A Third File.x"
or
SomeCommand --options A\ File.x Another\ File.x A\ Third\ File.x
However, I fail to simulate that in a script. So far I only tried the one
with quotes, though.
The script should be run as follows:
TheScript "A File.x" "Another File.x" "A Third File.x"
or
TheScript A\ File.x Another\ File.x A\ Third\ File.x
In this example there are three files, but that's just an example. There
could be ten or only one, for instance.
I 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
I thought that omitting quotes for FileList in SomeCommand would give me
the result I want, but it didn't.
The FileList creation might look a little ugly and hard to follow (all
those ' and "), so here is what it looks like when I add some newlines here
and there…
FileList=
"${FileList}"
'"'
"${FileName}"
'"'
" "
'"' is a double quote inside two single quotes, ' " ', to generate a
double quote in the FileList variable.
Obviously this doesn't work, as I said. But what works?
One idea I have to get around this problem, is to copy the files I need to
a new folder, and then use the * wildcard:
SomeCommand --options "$Folder"/*
And when the command is successfully finished, I could delete those copies
again.
That kind of workaround doesn't feel very good. One way could be to create
links to the original files instead of copying the files, then run the
command in some ”follow links mode”. Feels a little better than copying
files, but still not very good…
I would really want to know if it's possible to continue the path I already
started at.
Some commands support reading file names from a file, but not the command
that I'm trying to use in this case, I think.
Johnny Rosenberg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20131118/e3225901/attachment.html>
More information about the ubuntu-users
mailing list