Srini's Point of View

My thoughts on technology, food and other fancies…

Unix Aliases with variables in bash

A lot of times you are typing the same command again and again on the unix command line. Unix Aliases help to save time from typing out these long commands. Back when I was using C Shell, I could use parameters in aliases. For example I have this in my .cshrc file to find certain running processes-

alias chk "ps auxwww | grep \!:1"

running this would show me all commands which have java in them

$ chk java

Unfortunately this alias does not work in Bash. The work around is to create a function for that, rather than an alias, and then exported it, like this:

function chk { ps auxwww | grep "$1"; }  
export -f chk

Note -f switch to export: it tells it that you are exporting a function. Put this in your .bashrc and you are good to go

Comments