Bash – How to Use Spaces in a Bash Alias Name

aliasbashunix

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?

Best Answer

The Bash documentation states "For almost every purpose, shell functions are preferred over aliases." Here is a shell function that replaces ls and causes output to be piped to more if the argument consists of (only) -la.

ls() {
    if [[ $@ == "-la" ]]; then
        command ls -la | more
    else
        command ls "$@"
    fi
}

As a one-liner:

ls() { if [[ $@ == "-la" ]]; then command ls -la | more; else command ls "$@"; fi; }

Automatically pipe output:

ls -la
Related Question