meto
meto

Reputation: 3719

Aliasing a function with default parameters in bash

I would like to create an alias for an existing function to pass some parameters. Furthermore the alias should be able to take any flags of the original function and parse them correctly. In my specific case I am using ag.

When using zsh I can just add in my .zshrc file

alias -g ag="ag --nogroup --smart-case"

How can I obtain the same effect with bash only environments? The code

myfunc() {
    ag --nogroup --smart-case "$*"
}

alias ag=myfunc

works for the base case (i.e. ag hello) but does not accept parameters as in ag hello --context 2

Upvotes: 0

Views: 63

Answers (2)

glenn jackman
glenn jackman

Reputation: 247062

Since this case is simple enough (all the extra parameters go at the end) an alias will be sufficient:

alias ag="ag --nogroup --smart-case"

The -g option is not valid for bash alias.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799200

Using the correct parameter substitution is important.

ag() {
  command ag --nogroup --smart-case "$@"
}

Upvotes: 3

Related Questions