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?
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?
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.
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
csh
doesn't have functions. –
Prelate © 2022 - 2024 — McMap. All rights reserved.
csh
doesn't have functions, which is why aliases can accept parameters. (Or maybecsh
doesn't have functions because aliases can accept parameters; I'm not sure about cause and effect here.) – Prelate