Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want an alias ('ggg') that will look through my existing set of aliases and tell me all the ones that begin with g. I have a lot of g* aliases :)

I tried this: alias ggg='alias | grep ^g' but didn't give me any output (or error). The thing I'm most unsure about is the 'start of line' character.

share|improve this question
1  
In which shell? What does alias | grep ^g output? –  Gilles Nov 24 '12 at 14:45
    
That code above would work with ksh88, ksh93, pdksh, mksh, ash, dash, zsh, csh, tcsh... but not bash. –  Stéphane Chazelas Nov 24 '12 at 21:00
    
bash (tag added above). –  Michael Durrant Nov 24 '12 at 21:26
    
I would advise you not to use aliases for these trivial tasks, because 1) you need to remember them all, and need aliases to refresh them and 2) when you're on another system, you're constantly typing not found aliases. –  Bernhard Nov 24 '12 at 23:12

3 Answers 3

up vote 2 down vote accepted

The output of alias is lines starting with alias. So your alias should be:

alias ggg='alias | grep "^alias g"'
share|improve this answer
2  
True for bash, not for ksh or zsh. –  Gilles Nov 24 '12 at 14:45
    
Didn't know that as I use bash, thanks for the addition! –  Dennis Kaarsemaker Nov 24 '12 at 14:52

In zsh,

alias ggg='alias -m "g*"'

Or use a function, so that ggg doesn't print itself:

ggg() alias -m 'g*'

You could also grep the output of "alias", but it may not work properly if there are some multi-line aliases.

With bash, you could use this trick:

(
   alias() { [[ $1 = g* ]] && builtin alias "${1%%=*}"; }
   eval "$(builtin alias)"
)

The idea being that bash's alias outputs some text that is ready to be interpreted to reproduce the same aliases, something like:

$ alias
alias a='foo'
alias goo='gar
baz
alias gro=grar'

So we do evaluate it, but after having redefined alias as a function that calls the real alias only when passed a string that starts with "g".

share|improve this answer

It depends on your shell, if you are using bash @Dennis is completely right, for zsh it may be another issue if you enabled EXTENDED_GLOB in which case the ^ is interpreted by the shell and you have to quote it, i.e:

alias ggg='alias | grep "^g"'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.