3

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 to man 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.

  • It'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. – jw013 Nov 14 '14 at 15:37
  • @jw013 The first command makes no sense, i already said that. The second one reads man pages in cat. – DisplayName Nov 14 '14 at 15:48
  • 1
    I 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 to cat. – terdon Nov 14 '14 at 15:49
  • Yeah, i guess that makes sense. – DisplayName Nov 14 '14 at 15:50
  • 1
    @DisplayName ah, I see why you use it. OK, I guess. Note that you can simply run man grep | grep foo to match lines containing foo. I just figured that either you want to browse the man page in which case less 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 use man grep | cat which is equivalent to man -P cat grep. – terdon Nov 14 '14 at 15:59
6

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.

| improve this answer | |

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.