To create an alias for grep and to exclude svn directory (currently no questions cover this) on bash env I did:

$ function gp {grep -rnIi --exclude-dir='.svn';}

However, I get error

bash: syntax error near unexpected token `}'

How do I go about this?

  • Kusalananda showed you how to define a gp function, but he didn’t explicitly identify the error in your attempt, which is that { must be followed by a space (or a newline). – Scott Sep 10 '18 at 21:04
  • Thanks Scott and Kusalananda. I changed my code to and it works: function gp() { grep -rnIi --exclude-dir='.svn' $1 $2;} – piyush89 Sep 11 '18 at 17:17

function is not alias

alias gp="grep -rnIi --exclude-dir='.svn'"
  • 1
    As a function: gp () { grep ... "$@"; } – Kusalananda Sep 10 '18 at 20:02

I changed my code to:

function gp() { grep -rnIi --exclude-dir='.svn' $1 $2;}

and it works

  • It’s better to use "$@", as Kusalananda suggested. It automatically expands to all the arguments that are passed to the function (i.e., $1 $2 $3 $4 …), so you can run it on many files / directories at once, and it quotes them, so your pattern and filenames can have spaces and special characters. – Scott Sep 11 '18 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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