Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.