3

Consider the following command.

grep -rn "someString" . --color

And I want to alias it in my .cshrc and perform the command like this:

myGrep someString 

Is there a way to do this?

3
  • Have you tried alias mygrep='grep something..... ' Commented Aug 31, 2015 at 11:42
  • csh doesn't have functions, which is why aliases can accept parameters. (Or maybe csh doesn't have functions because aliases can accept parameters; I'm not sure about cause and effect here.) Commented Aug 31, 2015 at 13:17
  • 1
    Ugh. I though aliases accepted parameters; apparently, you just use history expansion to access arguments from the command prior to its alias expansion. Commented Aug 31, 2015 at 13:22

2 Answers 2

3

csh records your command in its history list prior to expanding aliases, so you can use history expansion to access arguments to the alias when it is used.

% alias myGrep grep -rn \!:1 . --color

When you use myGrep foo, that two-word command is recorded in history, then it is expanded to grep -rn !:1 . --color. In that command, !:1 refers to the first argument of the previous command (myGrep foo), resulting in grep -rn foo . --color, which is actually executed.

Sign up to request clarification or add additional context in comments.

Comments

-2

The proper way to make an alias that takes a parameter is with a function. In Bash functions the arguments are assessed as $1, $2 and so forth. Strung concatenation is implicit. Consider:

hello() {
    echo 'Hello' $1
}

I is simply called like this:

$ hello Eduard
Hello Eduard

1 Comment

csh doesn't have functions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.