How do I set an alias for a search command that takes two arguments

1

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?

alwbtc

Posted 2012-10-02T19:35:28.307

Reputation: 2 367

Answers

3

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.

Serge

Posted 2012-10-02T19:35:28.307

Reputation: 2 585

Functions are far better than aliases on several different levels. Listen to Serge and you'll go far. – UtahJarhead – 2012-10-02T20:22:15.417