PDA

View Full Version : UNIX alias question




Let's Sekuhara!
Jun 16, 2012, 12:21 AM
Hi, I'm new to UNIX and want to set up an alias called "trash" that will send files to ~/.Trash instead of deleting them like rm does.

I've found multiple websites stating that a command such as:
alias trash 'mv\!* ~/.Trash'
will do just that.

However when I run that command all I get is:
-bash: alias: mv\!* ~/.Trash: not found

Why? :confused:



subsonix
Jun 16, 2012, 03:25 AM
It works, in csh. Bash alias does not support arguments.

Let's Sekuhara!
Jun 16, 2012, 04:08 AM
It works, in csh. Bash alias does not support arguments.

Ah ha!
Thank you for shedding light on that.
Nothing I can do to work around that I suppose?

subsonix
Jun 16, 2012, 04:22 AM
Ah ha!
Thank you for shedding light on that.
Nothing I can do to work around that I suppose?

Yes, you can create a function that you define in your ~/.profile that takes the arguments, then make the alias call that function, I have never done that my self though.

But, to be honest it may be more straight forward to just create a small script that does what you want. This should do what that alias did, you can do more here as well if you want to, like suppress the error from "mv" and replace it with a custom error message if a file can't be found and so on.

Test and run at your own risk. :)


#!/bin/bash
#

if [ $# -lt 1 ]
then
echo "Usage: trash [filename]"
exit 0
fi
for i in "$@"
do
mv "$i" ~/.Trash
done


If you have folder in your $PATH where you place your scripts, just put it there, after you 'chmod u+x' it.

cqexbesd
Jun 17, 2012, 05:57 AM
Nothing I can do to work around that I suppose?

You could change your shell to tcsh and get the true BSD experience :)

ctrl-click your user in the "Accounts" pref pane and choose advanced. Then you can enter the path to any shell you like (such as /bin/tcsh).