Error 

Tagged as ‘Commands’

Suggested change to aliasing commands

Posted January 18, 2010 at 02:01am in Linux

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

Double Bang

Posted November 16, 2008 at 10:11pm in Sys Admin

No I’m not excited about sudo, well I sorta am. I have run into situations where I typed a long command and forgot to type sudo. This is a huge problem when you can’t hit the home key and go to the start of the command, which happens in a lot of SSH tools.

The Fix!!!

sudo !!

Typing that will execute the last command, but it will execute it as if you had typed sudo at the beginning. This is a huge time saver.

Bang Asterisk

Posted November 16, 2008 at 10:11pm in Sys Admin

Similar to the double bang trick is bang+asterisk. This combinations takes just the parameters from the previous command.

[user@host ~]$ ls /home
user
[user@host ~]$ cd !*
cd /home
[user@host home]$ 

Enjoy!