0

I'm reading the LinuxShellScriptingCookbookThirdEdition and practicing the code example from the book. The book gave the following command to replace rm with a back-up version.

alias rm='cp $@ ~/backup && rm $@'

however, when i put this line into my .bashrc and source it, the output after i ran rm was:

$ rm mail.txt    
cp: missing destination file operand after '/home/qiaozhuoyue/backup'
Try 'cp --help' for more information.

which seems like $@ was not expand as expected, then I used echo "cp $@ ~/backup && rm $@" instead, from the output I found that the position of the first $@ was blank, that is, the former didn't expand as expected.

$ rm mail.txt
cp  ~/backup && rm  mail.txt

what I expect output is:

$ rm mail.txt
cp mail.txt ~/backup && rm  mail.txt

Did I miss something?

caco jr
  • 27
  • 3
  • `$@` expands to shell's positional parameters, aliases don't take arguments. – oguz ismail Dec 22 '20 at 04:54
  • Wow, that really is [straight out of the book](https://subscription.packtpub.com/book/networking_and_servers/9781785881985/1/ch01lvl1sec16/visiting-aliases), and it doesn't work at all. I suddenly don't think much of this book. BTW, in addition to replacing it with a function (which *can* process arguments), you should also double-quote `"$@"` to avoid parsing weirdness. – Gordon Davisson Dec 22 '20 at 05:42

0 Answers0