I am trying to create an aliases in bash. What I want to do is map ls -la to ls -la | more

In my .bashrc file this is what I attempted:

alias 'ls -la'='ls -la | more'

However it does not work because (I assume) it has spaces in the alias name. Is there a work around for this?

link|improve this question

4  
Why not do alias lsm='ls -la | more' – Nifle Feb 5 '10 at 22:52
feedback

3 Answers

up vote 10 down vote accepted
ls() { if [[ $@ == "-la" ]]; then command ls -la | more; else command ls "$@"; fi; }
link|improve this answer
2  
and while this works, it is not an alias, but a function. – quack quixote Feb 6 '10 at 17:33
Thanks for the clarification. – Dennis Williamson Feb 6 '10 at 19:36
why do you need to use double brackets inside the if statement? – sixtyfootersdude Feb 8 '10 at 14:55
@sixtyfootersdude: The double-bracket form is more powerful and I use it by habit. See mywiki.wooledge.org/BashFAQ/031 – Dennis Williamson Feb 8 '10 at 18:35
feedback

From the alias man page:

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The alias name and the replacement text may contain any valid shell input, including shell metacharacters, with the exception that the alias name may not contain `='.

So, only the first word is checked for alias matches which makes multi-word aliases impossible. You may be able to write a shell script which checks the arguments and calls your command if they match and otherwise just calls the normal ls (See @Dennis Williamson's answer)

link|improve this answer
+1 For explaining why I am not allowed to use ls -la as an alias. – sixtyfootersdude Feb 8 '10 at 14:49
feedback

You can invoke this alias still, but you need quotation in order that the space is part of the command word. So "ls -la" -p pattern will pass the -p pattern option to more, not ls.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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