I want to create an Alias for my Terminal to quick-search inside files.

In Ubuntu is was: alias gre="egrep -rnw ". I used it like that $ gre 'my search string'.

Now I want this for my local MacBook-Terminal. I found out that I have to use grep only like this grep -r 'search string' .

How can I add an Alias like the one in Ubuntu?

I think it's something like this: alias gre="grep -r $theTerminalParameter ."

If I test it in the Terminal: alias test="grep -r $var ." and check the "content" of that alias with alias test i get that information:

alias test='grep -r .' so my Variable doesn't appear in the alias.

How can I use an Alias for that task.

  • 1
    alias gre='grep -r', and then use it as gre pattern or gre $VAR :-)
    – nohillside
    May 17 '18 at 10:49

You can't use alias for arbitrary substitutions. You can use a function instead.

function gre() {
  grep -r "$@" .
  • Thanks that works for me... others may need the information where to store this function: ~/.bash_profile is the right place. Don't forget to open a new tab to use the changes with cmd-t or quit and reopen the terminal.
    – MonTea
    May 17 '18 at 10:13
  • @MonTea Sure, but if you familiar with making aliases as is the premise of the question, then functions don't require any additional knowledge. A function can still be manually typed into the shell just like an alias, to take effect only in that session.
    – grg
    May 17 '18 at 12:11
  • @MonTea it is actually sufficient to source .bash_profile
    – dtell
    May 17 '18 at 19:42

You can add the line to ~/.bash_profile

alias gre='grep -rwn'

and use it just adding dot after search string

gre 'search string' .

to update your current session in case you do not want to close the tab

source ~/.bash_profile

which in turn your can alias as well if you like adding new aliases from time to time. sbash for example.

You must log in to answer this question.

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