> Is this a bug, or am I missing something?
[...]
> hacksaw >  ls
> -f  bar  baz  foo  fum
> 
> habitrail:/tmp/foo
> hacksaw >  rm -i *
> 
> habitrail:/tmp/foo
> hacksaw > ls
> -f

This is not a bug.  This is normal behavior.  The shell expands file
globs such as '*'.  The command that rm is seeing is the following.
You can test that with the echo command.  echo rm -i *

  rm -i -f bar baz foo fum

The -f option to rm overrides the -i option.  Therefore the files are
removed without asking.  Since the shell expands globs like '*' before
the program sees the command line it cannot distinguish between
something the user typed and something that was expanded by the shell.
The shell filters all command lines.  This is a good thing and adds a
lot of power to the system but it means you have to know that the
shell expansion filter is there to write robust scripts and command
lines.

> So, it happily removed all the files except -f, completely
> non-interactively.  This is certainly bad.

To robustly write a command that does what you are wanting you need to
do one of the following:

  rm -i ./*
or
  rm -i -- *

Bob Proulx

P.S. A common 'hack' is to create a file in a directory that is '-i'
which is exactly the reverse of what you were doing.  But then rm gets
the '-i' option when a user types in 'rm *' inadvertantly because
things expand and the final expanded command is 'rm -i file1 file2'.
I don't personally like this but it was so similar to your case but in
reverse that I had to comment.

Reply via email to