0

I find myself typing | grep and | less a lot, and was wondering if there was any way to be able to do something like:

alias G='| grep -E'  # egrep is deprecated
alias L='| less'

So I could do something like:

$ command G grepstring

or

$ verbose-command L

I currently transitioning over from bash to zsh, so please address both shells if possible.

2 Answers 2

4

bash

I don't know how to do this in bash, as the manual says:

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command.


zsh

help alias says:

If the -g flag is present, define a global alias; global aliases are expanded even if they do not occur in command position.

So in the less example:

alias -g L="| less"

Examples

zsh-lovers gives the following examples:

   alias -g ...='../..'
   alias -g ....='../../..'
   alias -g .....='../../../..'
   alias -g CA="2>&1 | cat -A"
   alias -g C='| wc -l'
   alias -g D="DISPLAY=:0.0"
   alias -g DN=/dev/null
   alias -g ED="export DISPLAY=:0.0"
   alias -g EG='|& egrep'
   alias -g EH='|& head'
   alias -g EL='|& less'
   alias -g ELS='|& less -S'
   alias -g ETL='|& tail -20'
   alias -g ET='|& tail'
   alias -g F=' | fmt -'
   alias -g G='| egrep'
   alias -g H='| head'
   alias -g HL='|& head -20'
   alias -g Sk="*~(*.bz2|*.gz|*.tgz|*.zip|*.z)"
   alias -g LL="2>&1 | less"
   alias -g L="| less"
   alias -g LS='| less -S'
   alias -g MM='| most'
   alias -g M='| more'
   alias -g NE="2> /dev/null"
   alias -g NS='| sort -n'
   alias -g NUL="> /dev/null 2>&1"
   alias -g PIPE='|'
   alias -g R=' > /c/aaa/tee.txt '
   alias -g RNS='| sort -nr'
   alias -g S='| sort'
   alias -g TL='| tail -20'
   alias -g T='| tail'
   alias -g US='| sort -u'
   alias -g VM=/var/log/messages
   alias -g X0G='| xargs -0 egrep'
   alias -g X0='| xargs -0'
   alias -g XG='| xargs egrep'
   alias -g X='| xargs'
3
  • The main problem with zsh is that it in it's default behavior has so many deviations from the standard that it is not even able to interpret a configure script, As a result, it is used by users that like e.g. csh
    – schily
    Commented Jun 30, 2018 at 8:48
  • I script in bash for portability, but find zsh great for interactive use.
    – Tom Hale
    Commented Jun 30, 2018 at 10:03
  • 1
    For portability, I don̈́'t write bash scripts and I enhanced the Bourne Shell to support what I need in interactive use.
    – schily
    Commented Jun 30, 2018 at 10:49
0

The Bourne Shell supports aliases that are expanded whenever the name is seen in the command line.

alias -a name=value

sets up such an alias. The concept goes back to an idea from 1980 on the first UNIX clone UNOS. Use with care since you need to escape name if you don't like it to be expanded.

See: http://schilytools.sourceforge.net/man/man1/bosh.1.html

The interface that is similar to the UNOS interface from 1980 is currently at page 6, the alias section at page 7 and the alias command is documented at page 36.

The Bourne Shell also implements the concept of persistent aliases from UNOS.

Since the aliases in the Bourne Shell are more poweful than in other shells, it is recommended to take some time and to play with that feature to understand what it could do.

4
  • I googled this and couldn't find a reference. Could you provide one?
    – Tom Hale
    Commented Jun 30, 2018 at 6:21
  • Google frequently does not help if you like to find something that is too wide spread. Best example: try to find information on star... you need to search for star tar if you like to get useful information. BTW: I added a URL to the man page to my answer.
    – schily
    Commented Jun 30, 2018 at 8:16
  • Thanks @schily. I wasn't aware of the Schily Bourne Shell project.
    – Tom Hale
    Commented Jul 1, 2018 at 7:33
  • Well it is strange that everybody seems to know the heirloom shell even though it got 3 months of work only and is not fully portable while bosh exists since 12 years and gets constant attention and enhancements. We did even fix dozens of very old bugs after identifying them with fuzzers and the rewrite made it one of the fastest shells. It is fine grained configurable from compatibility to the original Bourne Shell to beyond POSIX.
    – schily
    Commented Jul 1, 2018 at 10:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .