I've written a bash script that needs to do something later. It's called something like this:
later mv *.log /somewhere/else
However, when called like this *.log
is expanded at call time, and the script is called as if I wrote
later mv 1.log 2.log 3.log /somewhere/else
I'm trying to get my script to expand wildcards later. I tried calling it like this
later mv '*.log' /somewhere/else
and also with \*
, but these both result in the wildcard never getting expanded at all.
How do I expand the wildcards in the commandline? And is there a way to prevent expansion when the script is called, i.e. get the original parameters as they were typed?
This is the part of my scripts that prepares the call for later:
tmpfile=$(mktemp)
### Get a quoted commandline
line=
while (( "$#" > 0 )); do
line="$line\"$1\" "
shift
done
### Prepare a script to be run
echo '#!/bin/bash' > "$tmpfile"
echo "cd $(pwd)" >> "$tmpfile"
echo "trap 'rm \"$tmpfile\"' EXIT" >> "$tmpfile"
echo "$line" >> "$tmpfile"
chmod 777 "$tmpfile"
Note that I have to quote the commandline as some of my files and folders have spaces in their names; if I remove the quoting bit, even bits without wildcards stop working.
The simplest way may be to simply pass the wildcard (glob) back to the shell for expansion,
for example:
The
$( … )
operator calls a sub-shell, and interpolates the results; theecho
command simply returns them.eval
is neccesary within a shell script (but not when keying it in on an interactive shell, in which case$(echo $1)
works.Note that you'll still have to escape spacing and the like, but it seems you've taken that into consideration. Naturally, the glob will need to be quoted when calling your script — this is why most Unix commands only take one list in the first or last position(s), although examples like
find
do exist that require escaped fileglobs, themselves.PS: As for a program forcing the shell to not perform expansion: no, there's no built-in way to do so. The shell expands globs before (well, logically “before” if not necessarily chronologically) even identifying what file your program lives in, and is pretty well universally agnostic to what programs it runs.