Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 10 down vote accepted

Just redirect the output.

unalias rm mv cp >/dev/null 2>/dev/null
share|improve this answer
    
Of course! Thanks. – Villemoes Apr 30 '11 at 4:36
    
&>/dev/null will take out both birds with one stone. – Travis Sep 12 '15 at 4:18

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'
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .