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 a shell command which find files that contain 2 strings together:

$ grep -rl JFL . 2>/de/null | while read n; do grep -l 20120907 "$n"; done

The strings above are JFL and 20120907

Now I would like to set it as an alias, like below:

mysearch JFL 20120907

How can I create this alias?

share|improve this question
up vote 3 down vote accepted

add to your ~./bashrc the following:

mysearch () {
      grep -rl "$1" . 2>/dev/null | while read n; do grep -l "$2" "$n"; done
}

This defines the function that became available as a command at the bash prompt. All arguments to the functions in bash are expanded as $1, $2,... like arguments to the scripts. When editing your .bashrc do not forget to restart bash (re-login), as bash read this file when it starts.

share|improve this answer
    
Functions are far better than aliases on several different levels. Listen to Serge and you'll go far. – UtahJarhead Oct 2 '12 at 20:22

You must log in to answer this question.

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