Suggested change to aliasing commands
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
