Code:
function lx() {
ls --ignore='$1'
;}
$ lx *.jpg
This fails for two reasons. First, you hard-quoted the variable, so
ls sees only the literal pattern
'$1'. You need to use double-quotes to let the variable expand.
Second, the shell expands globbing patterns
before passing them to the command. so your function (after fixing the above) would actually execute as:
Code:
ls --ignore="file1.jpg file2.jpg file3.jpg"
You have to quote or escape the globbing pattern on the command line so that it gets passed literally.
( By the way, you should use either
function lx, or
lx(), but not both together. Bash ignores the error, but it's still bad syntax. Posix specifies
funcname() only. Also, the
; at the end is only needed to separate the closing bracket from a previous command on the same line. When the bracket is on a line of its own, the semicolon before it causes a syntax error. )
Code:
lx() {
ls --ignore="$1"
}
$ lx "*.jpg"
Another possibility would be to build the globbing pattern into the function itself.
Code:
lx() {
ls --ignore="*$1*"
}
$ lx .jpg
Note that the first option is more flexible, as it allows you to pass any globbing pattern to the ignore list, whereas the second one will only work reliably for literal text patterns. Also note that the function as written will only accept a single argument, and that you can't pass any other options to it.
A more robust function would look something like this:
Code:
lx() {
local x="$1" ; shift
ls --ignore="*$x*" "$@"
}
This allows other options to be passed to the command too. Note though that the ignored pattern must still always be the first argument given.
It might do you better in the long run to just learn how to use globs more effectively
http://mywiki.wooledge.org/glob
http://www.linuxjournal.com/content/...ended-globbing