Originally Posted by dazdaz
It works fine with a shell alias, but not a shell function.
Hi. I'd tell the opposite. What you expected is done by the function not by the alias: it is the logic not quite correct. Using eval
means execute its arguments as a command. If you do something like
eval somefile somedir
the shell tries to execute the command
that is obviously not correct. Regarding the alias and the function, in case of the alias you obtain
$ ll abc\ xyz/
ll abc\ xyz/
+ ls -l 'abc xyz/'
Here eval evaluates nothing, because $@ are the arguments (better, the positional parameters) to the ls -l
command, not to the eval subprocess. Evaluating a null string means to execute nothing, therefore no error is given. On the contrary in the function you obtain
$ lll abc\ xyz/
lll abc\ xyz/
+ lll 'abc xyz/'
++ eval abc xyz/
+++ abc xyz/
bash: abc: command not found
+ ls -l
Here the positional parameters are the arguments to the function and they are evaluated in any position (statement) inside the function itself. The eval command (in blue) evaluates the string "abc xyz" and try to execute it as a command, therefore the "command not found error". Then ls -l (in dark red) is executed with no arguments at all, because of the previous error.