shell quoting within a function
Hi all,
So here's a nice brain teaser. I am trying to write a shell function that will allow me to view files in directories with spaces or special characters. I am using eval to pad this out. It works fine with a shell alias, but not a shell function. I've tried so many variations, I need some guidance. Please help :-) $ mkdir abc\ xyz $ set -xv $ alias ll='ls -l `eval $@`' alias ll='ls -l `eval $@`' + alias 'll=ls -l `eval $@`' history -a ++ history -a $ ll abc\ xyz/ ll abc\ xyz/ eval $@ ++ eval + ls -l 'abc xyz/' total 0 -rw-r--r-- 1 root root 0 2007-09-02 00:53 testfile history -a ++ history -a $ function lll () { ls -l `eval $@`; } function lll () { ls -l `eval $@`; } history -a ++ history -a $ lll abc\ xyz/ lll abc\ xyz/ + lll 'abc xyz/' eval $@ ++ eval abc xyz/ abc xyz/ +++ abc xyz/ bash: abc: command not found + ls -l total 4944 -rw-r--r-- 1 root root 3837738 2007-04-10 23:36 allpackages.html -rw-r--r-- 1 root root 1036131 2007-04-10 21:04 debian4.0-r0-etch-dvd1 -rw-r--r-- 1 root root 90937 2007-06-23 22:08 report_all_plugins.html history -a ++ history -a |
Why would you want to "read ARGs as input to the shell and execute the resulting command" (aka 'eval') things when you use a default IFS and have already escaped space?
|
Quote:
Code:
eval somefile somedir Code:
somefile somedir Code:
$ ll abc\ xyz/ Code:
$ lll abc\ xyz/ |
It makes more sense now.
function lll () { ls -l "$@"; } Thanks. |
The key is quoting the $@, this causes the positional parameters (arguments) to seen as the separate words they were in the command line.
See man bash & do a search for 'Special Parameters'. |
All times are GMT -5. The time now is 10:42 AM. |