How do I define alias with variables which can be changed at runtime?

7,111

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.

Share:
7,111

Related videos on Youtube

DisplayName
Author by

DisplayName

Updated on September 18, 2022

Comments

  • DisplayName
    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 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.

    • jw013
      jw013 over 9 years
      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.
    • DisplayName
      DisplayName over 9 years
      @jw013 The first command makes no sense, i already said that. The second one reads man pages in cat.
    • terdon
      terdon over 9 years
      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.
    • DisplayName
      DisplayName over 9 years
      Yeah, i guess that makes sense.
    • DisplayName
      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
      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 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.
    • jimmij
      jimmij over 9 years
      If you are using less as a PAGER (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.