I have a bash function:
function some_function()
{
$*
}
I call this function like this:
some_function "cal 2014"
Why do I get a "cal 2014": not found error?
But it works if I call:
some_function "cal"
Let's examine the various behaviors here for a minute.
$ # Helper function to show some useful details.
$ c ()
{
printf 'argc: %s\n' "$#";
printf 'argv: %s\n' "$@"
}
$ # The function in question.
$ t ()
{
printf '$*\n';
c $*;
$*;
printf '"$*"\n';
c "$*";
"$*";
printf '$@\n';
c $@;
$@;
printf '"$@"\n';
c "$@";
"$@"
}
If we call the function like this t "echo foo" we get
$*
argc: 2
argv: echo
argv: foo
foo
"$*"
argc: 1
argv: echo foo
-bash: echo foo: command not found
$@
argc: 2
argv: echo
argv: foo
foo
"$@"
argc: 1
argv: echo foo
-bash: echo foo: command not found
$* and $@ versions expand to two arguments and work correctly."$*" and "$@" versions expand to a single argument and fail.What if we call it like this t echo foo?
$*
argc: 2
argv: echo
argv: foo
foo
"$*"
argc: 1
argv: echo foo
-bash: echo foo: command not found
$@
argc: 2
argv: echo
argv: foo
foo
"$@"
argc: 2
argv: echo
argv: foo
foo
$*, $@ and "$@" versions all expand to two arguments and work."$*" version expands to a single argument and fails.Ok. That makes sense since $* expands the arguments as individual (unquoted) words; "$*" expands to a single (quoted) word; $@ expands like $*; and "$@" expands to individual (quoted) words.
What about if we try this t echo "foo bar"?
$*
argc: 3
argv: echo
argv: foo
argv: bar
foo bar
"$*"
argc: 1
argv: echo foo bar
-bash: echo foo bar: command not found
$@
argc: 3
argv: echo
argv: foo
argv: bar
foo bar
"$@"
argc: 2
argv: echo
argv: foo bar
foo bar
Now things get interesting.
$* expands to three arguments. (Ok. I did say "individual (unquoted) words)" before.)"$*" expands to one argument. (Right, "single (quoted) word".)$@ expands to three arguments. (Right, same as $*.)"$@" expands to two arguments and keeps our internal space correctly. (As I said "individual (quoted) words)".)This is why, in general, you always quote variables and almost always want to use the @ expansions over the * expansions. Because they handle spaces and metacharacters correctly.
Ultimately though we didn't reproduce your problem when using $* directly. Are you sure you didn't use "$*" instead?
type <fun>.
eval $*.$*or did you use"$*"? That behavior sounds like"$*"not$*to me.