How do I define alias with variables which can be changed at runtime?
You need to use function, not alias, so that
mancat () { man "$1" | cat ; }
mancat grep
will do what you want.
Similarly
mygrep () { "$1" "$3" "$2" | "$1" -v "$4" | "$5" -n1; }
mygrep grep pattern1 file pattern2 head
mygrep grep pattern1 file pattern2 tail
will grep
for pattern1
in the file
and then select only lines which don't match pattern2
(grep -v
) and at the end select only first (or last) line.
Related videos on Youtube
![DisplayName](https://i.stack.imgur.com/JQWu1.png?s=256&g=1)
DisplayName
Updated on September 18, 2022Comments
-
DisplayName almost 2 years
This is probably a very easy to answer question, but I could not find any questions already asking this due to different wording when writing titles.
Running
help alias
on my bash prompt returns only this:alias: alias [-p] [name[=value] ... ]
Then a very short text that has nothing to do with what I'm asking.
I also tried:
help function
But that didn't give me much information either.
For example:
alias mancat="man command | cat"
So that I could run
mancat grep
which would be equivalent toman grep | cat
.I know that is called variables, but they are undefined and I would want to be able change them at any time like when running my example command.
-
jw013 over 9 yearsIt's not clear exactly what your example commands are supposed to do, but try taking a second look at functions. Functions are basically more powerful versions of aliases.
-
DisplayName over 9 years@jw013 The first command makes no sense, i already said that. The second one reads
man
pages incat
. -
terdon over 9 yearsI removed your first example since, as you pointed out, it made no sense and only confused things. Note that your second example doesn't make much sense either but at least it would run. Unless you want to use something like
cat -n
, there's never any point to piping tocat
. -
DisplayName over 9 yearsYeah, i guess that makes sense.
-
DisplayName over 9 years@terdon I know the comments aren't supposed to be discussions but why not? I like reading in cat more because it prints out the entire thing, I don't have to scroll and I can run commands in the same tab, (unlike using
less
which is a constant process). -
terdon over 9 years@DisplayName ah, I see why you use it. OK, I guess. Note that you can simply run
man grep | grep foo
to match lines containingfoo
. I just figured that either you want to browse the man page in which caseless
is much better since it can scroll with a mouse wheel and also allows searching or you want to parse the manpage in which case you can just pipe the output directly. If you want to dump the whole thing to your screen, you can indeed useman grep | cat
which is equivalent toman -P cat grep
. -
jimmij over 9 yearsIf you are using
less
as aPAGER
(MANPAGER
) then note that it check if its output is connected to terminal and only then stops at one screen at a time, highlight output in colours etc. Otherwise just prints everything to the stdout.
-