Alias with input. Unix
Asked Answered
P

2

1

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?

Plenum answered 31/8, 2015 at 11:32 Comment(3)
Have you tried alias mygrep='grep something..... 'Hardigg
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.)Prelate
Ugh. I though aliases accepted parameters; apparently, you just use history expansion to access arguments from the command prior to its alias expansion.Prelate
P
2

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.

Prelate answered 31/8, 2015 at 13:25 Comment(0)
O
-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
Osteomyelitis answered 31/8, 2015 at 11:44 Comment(1)
csh doesn't have functions.Prelate

© 2022 - 2024 — McMap. All rights reserved.