Error 

Suggested change to aliasing commands

210 views
Posted January 18, 2010 at 02:01am in Linux with tags , ,

When I wrote the code to automatically generate aliases for hosts in my SSH config I started thinking about how checks are never done to verify we are not overriding an existing alias. This is my solution for it. I almost think that alias should be an alias for register_alias so that all aliases get checked, but I’m sure there could be an instance where it would break something. Let me know what you think about assigning aliases this way.

# .bash_functions
function register_alias() {
  local alias=$(echo $* | cut -d'=' -f 1)
  local TEMP_CMD=$(which $alias)
  local TEMP_ALIAS=$(echo "`alias`" | sed 's/alias\ \(.*\)=.*/\1/' | grep ^$alias$)
  if [ ${#TEMP_CMD} -gt 0 ]; then
    echo "Alias $alias conflicts with command $TEMP_CMD"
  elif [ ${#TEMP_ALIAS} -gt 0 ]; then
    echo "Alias $alias conflicts with alias $alias"
  else
    alias "$*"
  fi
}
# .bashrc
if [ -f ~/.bash_functions ]; then
  . ~/.bash_functions
fi
# .bash_aliases

register_alias sls='screen -ls'
register_alias sdr='screen -d -r'

Update: I changed the TEMP_ALIAS line to use sed instead of cut/sed

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Twitter


Leave a Reply