I have access to a couple of remote accounts, which I mount via sshfs in subdirs of $HOME/SSHFS. In order to remind me that I'm not working with local files, I have set $PROMPT_COMMAND to a function prmt_cmd, where I set a different $PS1 if $PWD matches $HOME/SSHFS*. Now, to add a little extra protection, I'd like to add the -i flags automatically to the rm, mv and cp commands whenever I'm in a subdir of $HOME/SSHFS. So I ended up with something like

function prmt_cmd () {
if [[ $PWD == $HOME/SSHFS* ]] ; then
    PS1=some prompt
    alias rm='rm -I'
    alias mv='mv -i'
    alias cp='cp -i'
else
    # Reset the PS1, remove aliases
    PS1=my default prompt
    unalias rm mv cp
fi
}

(In the actual definition, I do some other stuff, but this is irrelevant.) However, unalias complains when the aliases do not exist, meaning that I get three error messages before each prompt when I'm not in SSHFS/*. unalias doesn't seem to have a switch to silence it. So my question is: Is there a better way to achieve what I want? Maybe I'm simply Doing It (Completely) Wrong.

In case it's relevant, $BASH_VERSION = 4.1.5(1)-release on Ubuntu 10.10.

link|improve this question
feedback

2 Answers

up vote 5 down vote accepted

Just redirect the output.

unalias rm mv cp >/dev/null 2>/dev/null
link|improve this answer
Of course! Thanks. – Villemoes Apr 30 '11 at 4:36
feedback

In bash you could do the following to unalias an alias only if it exists, for example rm:

[ -n "`alias -p | grep '^alias rm='`" ] && unalias rm

Another idea would be to overwrite the alias, even it if exists:

alias rm='/bin/rm'
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.